Topic Description :
Please implement a function to determine whether a binary tree is symmetrical. Note that if a binary tree is the same as the mirror image of the binary tree, it is defined as symmetric.
thought Analysis : The judgment is not the empty knot point, yes, directly, it's symmetrical. Determine whether the left and right subtrees on either side are symmetric and the left and right subtrees are empty, return true, have one null and false, and determine whether two nodes are equal, and then the left subtree is equal to the right-hand subtree of the Saozi tree. Two equal in the middle.
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)/NULL is directly symmetric
return true;
Return issymmetrical (Proot.right,proot.left);
}
Boolean issymmetrical (TreeNode Right,treenode left) {//To determine whether or not to
be equal if (right==null && left==null)/left and right subtree are empty return
true;
if (Right==null | | left==null)//left and right subtree has an empty return
false;
The right and left nodes are equal and the points to the left of the right subtree are equal, and the middle part is equal to return
left.val==right.val&&issymmetrical (Right.right, Left.left) && issymmetrical (left.right,left.right);
}