/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){return true;}
return isSymmetric2(root.left, root.right);
}
private boolean isSymmetric2(TreeNode left, TreeNode right){
if (left == null && right == null){return true;}
if ((left == null && right != null) || (left !=null && right == null)){
return false;
}
if (left.val == right.val){
return isSymmetric2(left.left, right.right) && isSymmetric2(left.right, right.left);
}
return false;
}
}
剑指 Offer 28. 对称的二叉树
评论