面试题28:对称二叉树
2018-12-27 本文已影响0人
灰化肥发黑会挥发
判断一颗二叉树是不是对称二叉树
- 思路:该题的思路为与上一题相似,递归判断左右子树。
import javax.swing.tree.TreeNode;
public class isSymmetrical {
public boolean judgeSymmetrical(TreeNode root){
if(root==null) return true;
return isSizeSummetrical(root,root);
}
public boolean isSizeSummetrical(TreeNode left,TreeNode right){
if(left==null&&right==null) return true;
if(left==null||right==null) return false;
if(left.val!=right=val) return false;
else
return isSizeSummetrical(left.left,right.right)&&isSizeSummetrical(left.right,right,left);
}
}