题目
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
判断一棵树是不是本身的镜像
1 首先想到递归。很简单的想法。左边等于右边,右边等于左边。
2 要注意的是需要两个参数,来进行镜像判断;这是画了几个例子后,写出来的。
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return useme(root,root);
}
public boolean useme(TreeNode left, TreeNode right){
if(left==null && right ==null){
return true;
}
if(left == null || right == null){
return false;
}
return left.val==right.val && useme(left.left,right.right) && useme(left.right,right.left);
}
}