题解 | #判断二叉树是否对称#
判断二叉树是否对称
http://www.nowcoder.com/practice/1b0b7f371eae4204bc4a7570c84c2de1
要判断左子树的左分支和右子树的右分支是否相等;
要判断左子树的右分支和右子树的左分支是否相等。
public class Solution {
/**
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isSymmetric (TreeNode root) {
// write code here
if(root==null)
return true;
return helper(root.left,root.right);
}
public boolean helper(TreeNode left , TreeNode right){
if(left==null&&right==null)
return true;
if(left==null||right==null)
return false;
if(right.val!=left.val)
return false;
return helper(left.left,right.right) && helper(left.right,right.left);
}
}
传音控股公司福利 355人发布


查看1道真题和解析