2019独角兽企业重金招聘Python工程师标准>>>
这题的思路也是递归(递归在二叉树中很重要哦)。
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root ==null){
return true;
}
return symmetric (root.right,root.left);
}
private boolean symmetric(TreeNode right,TreeNode left){
if(left == null && right == null) return true;
if(left == null || right == null) return false;
return (left.val==right.val)&&symmetric(left.left,right.right)&&symmetric(left.right,right.left);
}
}