剑指Offer--对称二叉树(JS)

题目描述

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function isSymmetrical(pRoot){
    // write code here
    return pRoot==null || judge(pRoot.left,pRoot.right)
}
function judge(node1, node2){
    if(node1==null && node2==null){
        return true
    }else if(node1==null || node2==null){
        return false
    }
    if(node1.val!==node2.val){
        return false
    }else{
        return judge(node1.left,node2.right) && judge(node1.right,node2.left)
    }
}
发布了69 篇原创文章 · 获赞 52 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_38983511/article/details/103000576