题解 | #对称的二叉树#

对称的二叉树

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

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
//传入两颗树root 同时对两棵树进行“先序”遍历(这样既判断了树的结构是否一致,也判断了树的值是否相等)
function preOrder(root1,root2){
    if(root1 == null && root2 == null){
        return true;
    }
    if(root1 == null || root2 == null || root1.val != root2.val){
        return false;
    }
    return preOrder(root1.left,root2.right) && preOrder(root1.right,root2.left);
}
function isSymmetrical(pRoot)
{
    // write code here
    return preOrder(pRoot,pRoot);
}
module.exports = {
    isSymmetrical : isSymmetrical
};

#我的实习求职记录#
全部评论

相关推荐

03-23 23:00
黄淮学院 Java
才浅Caiq:老家县城送外卖也5000,要求别这么低
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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