-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (36 loc) · 914 Bytes
/
Solution.java
File metadata and controls
39 lines (36 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package SymmetricTree;
import commons.datastructures.TreeNode;
/**
* User: Danyang
* Date: 1/19/2015
* Time: 19:21
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*/
public class Solution {
/**
* iterative: bfs, array sysmetric
* recursive: drawing the tree
* 1
* 2 2
* 3 4 4 3
* 5 6 7 88 7 6 5
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return isSymmetric(root.left, root.right);
}
boolean isSymmetric(TreeNode l, TreeNode r) {
if(l==null&&r==null)
return true;
try {
return l.val==r.val && isSymmetric(l.left, r.right) && isSymmetric(l.right, r.left);
}
catch(Exception e) {
return false;
}
}
}