forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Vertical Order Traversal.java
More file actions
213 lines (174 loc) · 5.41 KB
/
Binary Tree Vertical Order Traversal.java
File metadata and controls
213 lines (174 loc) · 5.41 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
M
1532015904
tags: BFS, DFS, Hash Table, Tree
time: O(n)
space: O(n)
给一个Binary Tree, traverse所有node, 按照vertial order 排列成output: List<List>
重点是: col里面有排序, 在higher level的排在前面; 如果node遇到collision在同一个位置: 根据他们的相对位置 先放left, 再放right
#### BFS
- 应该比较好想: naturally level-traverse all nodes, add node to appropriate col list
- Use min/max to track map keys, since the keys are continous
- Map does not provide random access; unless map key is marked with sequence i = [min, max]
#### DFS
- 一开始很容易想到: enumerate一下, 先放curr node.val, 然后node.left.val, node.right.val. 非常简单
- 但是最简单的方法有错: assume所有left subtree都 排在right subtree. 但是: right subtree可能先有一个lower-left-branch, appear in a column first.
- 所以还要preserve column list的order.
- 这里我们用了 `Map<col, Node>` 来track col, Node 里面用了 `node.level`来track level (其实再一个map也可以)
- 这样在结尾要sort,就会非常慢: Visit all nodes O(n) + O(logK) + O(KlogM), K = # of cols, M = # of items in col
- 应该也是可以optimize map keys的, 反正都是continuous key
```
/*
Given a binary tree, return the vertical order traversal of its nodes' values.
(ie, from top to bottom, column by column).
If two nodes are in the same row and column, the order should be from left to right.
Examples 1:
Input: [3,9,20,null,null,15,7]
3
/\
/ \
9 20
/\
/ \
15 7
Output:
[
[9],
[3,15],
[20],
[7]
]
Examples 2:
Input: [3,9,8,4,0,1,7]
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
Output:
[
[4],
[9],
[3,0,1],
[8],
[7]
]
Examples 3:
Input: [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5)
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
/\
/ \
5 2
Output:
[
[4],
[9,5],
[3,0,1],
[8,2],
[7]
]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
BFS: naturally level-traverse all nodes, add node to appropriate col list
Use min/max to track map keys, since it's continous
*/
class Solution {
class Node {
int col;
TreeNode treeNode;
public Node (int col, TreeNode treeNode) {
this.col = col;
this.treeNode = treeNode;
}
}
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> rst = new ArrayList<>();
if (root == null) return rst;
int min = 0, max = 0;
Map<Integer, List<Integer>> map = new HashMap<>();
Queue<Node> queue = new LinkedList<>();
queue.offer(new Node(0, root));
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node node = queue.poll();
map.putIfAbsent(node.col, new ArrayList<>());
map.get(node.col).add(node.treeNode.val);
if (node.treeNode.left != null) queue.offer(new Node(node.col - 1, node.treeNode.left));
if (node.treeNode.right != null) queue.offer(new Node(node.col + 1, node.treeNode.right));
min = Math.min(min, node.col);
max = Math.max(max, node.col);
}
}
for (int col = min; col <= max; col++) {
rst.add(map.get(col));
}
return rst;
}
}
/*
Use Map<Col Integer, List<Integer>> to track cols
dfs: add curr at col, dfs(col-1, node.left), dfs(col+1, node.right);
at end: sort map.keySet, and out
DFS can fail, if we assume `node.left subtree` always appears on top of `node.right subtree`
Therefore, between each column, node should have order as well: level
*/
class Solution {
class Node {
int val, level;
public Node (int val, int level) {
this.val = val;
this.level = level;
}
}
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> rst = new ArrayList<>();
if (root == null) return rst;
Map<Integer, List<Node>> map = new HashMap<>();
map.putIfAbsent(0, new ArrayList<>());
map.get(0).add(new Node(root.val, 0));
dfs(map, root, 0, 0);
List<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
for (int key : keys) {
List<Node> nodes = map.get(key);
nodes.sort(Comparator.comparing(a -> a.level));
List<Integer> col = new ArrayList<>();
for (Node node : nodes) {
col.add(node.val);
}
rst.add(col);
}
return rst;
}
public void dfs(Map<Integer, List<Node>> map, TreeNode node, int col, int level) {
if (node == null) return;
if (node.left != null) {
map.putIfAbsent(col - 1, new ArrayList<>());
map.get(col - 1).add(new Node(node.left.val, level + 1));
}
if (node.right != null) {
map.putIfAbsent(col + 1, new ArrayList<>());
map.get(col + 1).add(new Node(node.right.val, level + 1));
}
dfs(map, node.left, col - 1, level + 1);
dfs(map, node.right, col + 1, level + 1);
}
}
```