-
Notifications
You must be signed in to change notification settings - Fork 2
/
314-binary-tree-vertical-order-traversal.java
50 lines (44 loc) · 1.51 KB
/
314-binary-tree-vertical-order-traversal.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
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> output = new ArrayList<>();
if (root == null)
return new ArrayList<>();
Map<Integer, List<Integer>> map = new HashMap<>();
Queue<Pair<TreeNode, Integer>> que = new LinkedList<>();
que.add(new Pair(root, 0));
while(!que.isEmpty()) {
Pair<TreeNode, Integer> pair = que.remove();
root = pair.getKey();
int column = pair.getValue();
if (!map.containsKey(column))
map.put(column, new ArrayList<>());
map.get(column).add(root.val);
if (root.left != null)
que.add(new Pair(root.left, column-1));
if (root.right != null)
que.add(new Pair(root.right, column+1));
}
List<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
for (int key : keys) {
output.add(map.get(key));
}
return output;
}
}