Offer to prove safety fifty-eight: symmetrical binary tree

Casual working

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.

Thinking

When it is determined recursively given two nodes, if all null, meaning not exist, it means symmetry, if a null is not a null, represents an asymmetric, when determining if there are two values are equal, We are not equal, nor symmetrical.
For about the same two nodes of the root is beginning to enter the judge, and later in the recursive call on the need to find a symmetrical node to complete the required subject.

Code

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot==null)
            return true;
        return issys(pRoot.left,pRoot.right);
      
    }
    private boolean issys(TreeNode l,TreeNode r){
        if(l==null && r==null){
            return true;
        }
        if(l==null ||r==null){
            return false;
        }
        if(l.val!=r.val)
        {  return false;}
          
        return issys(l.left,r.right)&& issys(l.right,r.left);
    }
}
Published 84 original articles · won praise 53 · views 7386

Guess you like

Origin blog.csdn.net/weixin_44015043/article/details/105422217