题解 | #对称的二叉树#

对称的二叉树

https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb

/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return bool布尔型
 */
function isSymmetrical( pRoot ) {
    // write code here
    return boolean(pRoot,pRoot)
}
function boolean(a,b){
    //当节点都为空,同时为空,符合
    if(a == null &&b == null){
        return true
    }
    //当其中一个为空,不符合
    if(a == null || b == null){
        return false
    }
    //当他们的值不相等的时候,不符合
    if(a.val != b.val){
        return false
    }

    return boolean(a.left,b.right)&&boolean(a.right,b.left)
}
module.exports = {
    isSymmetrical : isSymmetrical
};

全部评论

相关推荐

职场水母:为啥你们整简历都喜欢整一大堆没用的,是期待让hr觉得很多,自己很厉害吗
0offer是寒冬太冷还...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务