题目: 请实现一个函数,用来判断一颗二叉树是不是对称的。
* 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
分析:此题比较简单 ,就简单的递归
public class isSymmetrical1 {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
boolean isSymmetrical(TreeNode pRoot)
{
return judge(pRoot,pRoot);
}
public boolean judge(TreeNode l, TreeNode r) {
if(l==null&&r==null){
return true;
}
if(l==null||r==null){
return false;
}
if(l.val!=r.val){
return false;
}
return judge(l.left,r.right)&&judge(l.right,r.left);
}
}