Symmetrical binary tree ---

Please implement a function, a binary tree is used to determine not symmetrical. Note that if a binary image is a binary tree with this same definition as symmetrical.

Analysis: binary tree is symmetric with respect to the left and right sides of the root of the intermediate symmetrical 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)

}

 

Guess you like

Origin www.cnblogs.com/mlebk/p/12632485.html