Tree 3: Please implement a function, a binary tree is used to determine not symmetrical. Note that if a binary image is a binary tree of this with the same, which is defined as the symmetrical

Symmetrical tree:

 

 

=================== // recursive algorithm =========================== // ==

1. As soon as pRoot.left are symmetrical and pRoot.right
2. The left node equal values and symmetrical subtree left.left, right.right; left.rigth, right.left also symmetrical
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

    }

}
*/
public class Solution {
    public boolean jude(TreeNode node1,TreeNode 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 jude(node1.left,node2.right)&&jude(node1.right,node2.left);
        }
    }
    public boolean isSymmetrical(TreeNode pRoot)
    {
        return pRoot==null||jude(pRoot.left,pRoot.right);
    }
}

// =================== non-recursive algorithm, the use of DFS and BFS ===================== ======== //

 

Guess you like

Origin www.cnblogs.com/xuechengmeigui/p/12631325.html