请实现一个函数,用来判断一棵二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
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)return true;
return isSame(pRoot,pRoot);
}
public boolean isSame(TreeNode pRoot1,TreeNode pRoot2){
if(pRoot1 == null && pRoot2 == null)return true;
if(pRoot1 == null || pRoot2 == null)return false;
return pRoot1.val == pRoot2.val && isSame(pRoot1.left,pRoot2.right) && isSame(pRoot2.right,pRoot1.left);//若满足两结点值相等且pRoot1的左树和pRoot2的右树,pRoot1的右树和pRoot2的左树
}
}