101. Symmetric Tree
给定一个二叉树,检查它是否是它自己的镜子(即,围绕它的中心对称)。
例如,这个二叉树[1,2,2,3,4,4,3]是对称的:
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/
2 2
/ \ /
3 4 4 3
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode rootLeft,TreeNode rootRight) {
if(rootLeft==null&&rootRight==null)
return true;
if(rootLeft==null||rootRight==null||rootLeft.val!=rootRight.val)
return false;
return isSymmetric(rootLeft.left,rootRight.right)&&isSymmetric(rootRight.left,rootLeft.right);
}
}