You've successfully subscribed to The Daily Awesome
Welcome back! You've successfully signed in.
Success! Your billing info is updated.
Billing info update failed.

# 101. 对称二叉树 - Symmetric Tree

## 101. 对称二叉树 - Symmetric Tree

[Level Tranversal]

给定一个二叉树，检查它是否是镜像对称的。


### Example:

    1
/ \
2   2
/ \ / \
3  4 4  3


    1
/ \
2   2
\   \
3    3


## solution

### My solution

/**
* 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) {
return isMirrorTree(root, root);
}

public boolean isMirrorTree(TreeNode left, TreeNode right) {
if(left == null)
return right == null;
if(right == null)
return left == null;
if(left.val == right.val)
return isMirrorTree(left.left, right.right) && isMirrorTree(left.right, right.left);
return false;
}
}


### Other Solution

class Solution {
public boolean isSymmetric(TreeNode root) {
if(root ==null)
return true;
return traversal(root.left,root.right);
}
public boolean traversal(TreeNode l,TreeNode r){
if(l ==null || r==null)
return l==r;
if(l.val != r.val)
return false;
boolean ret= traversal(l.left,r.right) && traversal(l.right,r.left);
return ret;
}

}