树---对称的二叉树

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

分析:对称二叉树就是相对于中间的根左右两边对称left.left==right.right&&left.right==right.left

/* function TreeNode(x) {

    this.val = x;

    this.left = null;

    this.right = null;

} */

function isSymmetrical(pRoot)

{

    // write code here

    if(pRoot===null){

        return true

    }

    return check(pRoot.left,pRoot.right)

}

function check(left,right){

    if(left===null){

        return right===null

    }

    if(right===null){

        return false

    }

    if(left.val!==right.val){

        return false

    }

    return check(left.left,right.right)&&check(left.right)

}

猜你喜欢

转载自www.cnblogs.com/mlebk/p/12632485.html