要判断左子树的左分支和右子树的右分支是否相等;
要判断左子树的右分支和右子树的左分支是否相等。
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);
}
}
京公网安备 11010502036488号