-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
ZigZagOrderLevelTraversalBST.java
106 lines (93 loc) · 3.11 KB
/
ZigZagOrderLevelTraversalBST.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
package problems.medium;
import problems.utils.TreeNode;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by sherxon on 1/8/17.
*/
@SuppressWarnings("Duplicates")
public class ZigZagOrderLevelTraversalBST {
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
System.out.println(new ZigZagOrderLevelTraversalBST().zigzagLevelOrder2(root));
}
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null) return list;
List<Integer> l = new LinkedList<>();
l.add(root.val);
list.add(l);
LinkedList<TreeNode> q = new LinkedList<>();
int level = 1;
q.add(root);
boolean flag = false;
while (!q.isEmpty()) {
TreeNode x = q.removeFirst();
if (x.right != null) q.addLast(x.right);
if (x.left != null) q.addLast(x.left);
level--;
if (level == 0 && !q.isEmpty()) {
LinkedList<Integer> ll = new LinkedList<>();
level += q.size();
for (TreeNode xx : q) {
if (flag)
ll.addFirst(xx.val);
else
ll.addLast(xx.val);
}
flag = !flag;
list.add(ll);
}
}
return list;
}
public String zigzagLevelOrder2(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null)
return "";
List<Integer> l = new LinkedList<>();
l.add(root.val);
list.add(l);
LinkedList<TreeNode> q = new LinkedList<>();
int level = 1;
q.add(root);
boolean flag = false;
while (!q.isEmpty()) {
TreeNode x = q.removeFirst();
if (x.right != null)
q.addLast(x.right);
if (x.left != null)
q.addLast(x.left);
level--;
if (level == 0 && !q.isEmpty()) {
LinkedList<Integer> ll = new LinkedList<>();
level += q.size();
for (TreeNode xx : q) {
if (flag)
ll.addFirst(xx.val);
else
ll.addLast(xx.val);
}
flag = !flag;
list.add(ll);
}
}
StringBuilder builder = new StringBuilder();
for (int i = list.size() - 1; i >= 0; i--) {
List<Integer> integers = list.get(i);
if (list.size() % 2 != 0) {
Collections.reverse(integers);
}
builder.append(integers.stream()
.map(String::valueOf)
.collect(Collectors.joining(","))
);
builder.append(",");
}
return builder.toString().substring(0, builder.length() - 1);
}
}