题目
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
思路
写一个函数判断左右子树是否对称
代码
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot == null)
return true;
return judge(pRoot.left,pRoot.right);
}
public boolean judge(TreeNode L, TreeNode R){
if(L == null && R == null)
return true;
else if(L == null || R == null){
return false;
}
if(L.val != R.val)
return false;
else
return judge(L.left,R.right) && judge(L.right,R.left);
}
}