-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101_SymmetricTree101.java
137 lines (119 loc) · 3.66 KB
/
101_SymmetricTree101.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* 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
*
* Note:
* Bonus points if you could solve it both recursively and iteratively.
*
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class SymmetricTree101 {
public boolean isSymmetric(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) return true;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root.left);
queue.add(root.right);
int i = 1;
while (!queue.isEmpty()) {
Stack<TreeNode> st = new Stack<>();
i = queue.size() >> 1;
int k = 0;
while (k<i) {
if (queue.isEmpty()) return false;
TreeNode node = queue.remove();
if (node != null) {
queue.add(node.left);
queue.add(node.right);
}
st.push(node);
k++;
}
k = 0;
while (k<i) {
if (queue.isEmpty()) return false;
TreeNode node = queue.remove();
TreeNode pre = st.pop();
if (node != null) {
if (pre == null || node.val != pre.val) return false;
queue.add(node.left);
queue.add(node.right);
} else if (pre != null) {
return false;
}
k++;
}
}
return true;
}
/**
* https://discuss.leetcode.com/topic/5941/recursive-and-non-recursive-solutions-in-java
*/
public boolean isSymmetric2(TreeNode root) {
if (root==null) return true;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode left, right;
if (root.left != null) {
if (root.right == null) return false;
stack.push(root.left);
stack.push(root.right);
} else if (root.right != null) {
return false;
}
while (!stack.empty()) {
if (stack.size()%2 != 0) return false;
right = stack.pop();
left = stack.pop();
if(right.val != left.val) return false;
if (left.left != null) {
if(right.right == null) return false;
stack.push(left.left);
stack.push(right.right);
} else if(right.right != null){
return false;
}
if (left.right != null) {
if(right.left == null) return false;
stack.push(left.right);
stack.push(right.left);
} else if(right.left != null) {
return false;
}
}
return true;
}
/**
* https://discuss.leetcode.com/topic/5941/recursive-and-non-recursive-solutions-in-java
*/
public boolean isSymmetric3(TreeNode root) {
return root==null || isSymmetricHelp(root.left, root.right);
}
private boolean isSymmetricHelp(TreeNode left, TreeNode right){
if(left==null || right==null)
return left==right;
if(left.val!=right.val)
return false;
return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);
}
}