请实现一个函数,用来判断一棵二叉树是不是对称的。
提示:如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public static boolean isSymmetrical(TreeNode pRoot){
return isSame(pRoot, pRoot);
}
// 判断一棵树和其镜像是否相同
private static boolean isSame(TreeNode root1, TreeNode root2){
if(root1 == null && root2 == null) return true;
if(root1 == null || root2 == null) return false;
return root1.val == root2.val &&
isSame(root1.left,root2.right) &&
isSame(root1.right,root2.left);
}
}