题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
思路
复制一个相同的二叉树,比较左子节点和右子节点的值,使用递归完成
代码
boolean isSymmetrical(TreeNode pRoot){
if(pRoot == null)
return true;
return real(pRoot.left,pRoot.right);
}
public boolean real(TreeNode root1,TreeNode root2){
if(root1 == null && root2 == null){
return true;
}
if(root1 ==null || root2 == null){
return false;
}
if(root1.val != root2.val){
return false;
}
return real(root1.left,root2.right)&&real(root1.right,root2.left);
}