-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathsymetricTree.java
70 lines (54 loc) · 1.68 KB
/
symetricTree.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
//O(n) because we traverse all of the nodes in the tree
//O(n) because the recursive calls will be the height of thee true, and in the worst case, tree is unbalanceed list
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymetric(root.left, root.right);
}
public boolean isSymetric(TreeNode left, TreeNode right)
{
if(left == null || right == null)
{
return left == right;
}
if(left.val!=right.val) //different values so cant be symetric
{
return false;
}
return isSymetric(left.left, right.right) && isSymetric(left.right, right.left);
}
}
//iteerative
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
Queue<TreeNode> q = new LinkedList();
q.add(root.left);
q.add(root.right);
while(!q.isEmpty()){
TreeNode left = q.poll();
TreeNode right = q.poll();
if(left == null && right == null)
continue;
if(left == null || right == null ||left.val != right.val )
return false;
q.add(left.left);
q.add(right.right);
q.add(left.right);
q.add(right.left);
}
return true;
}