-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-987-Vertical-Order-Traversal-of-a-Binary-Tree.java
More file actions
73 lines (62 loc) · 2.08 KB
/
LeetCode-987-Vertical-Order-Traversal-of-a-Binary-Tree.java
File metadata and controls
73 lines (62 loc) · 2.08 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
/**
* 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 {
// 1. DFS + Global Sorting
public List<List<Integer>> verticalTraversal(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;
List<int[]> nodes = new ArrayList<>();
DFS(root, 0, 0, nodes);
Comparator<int[]> cmp = new Comparator<int[]>() {
public int compare(int[] arr1, int[] arr2) {
if (arr1[0] == arr2[0]) {
if (arr1[1] == arr2[1]) {
return arr1[2] - arr2[2];
} else {
return arr1[1] - arr2[1];
}
} else {
return arr1[0] - arr2[0];
}
}
};
Collections.sort(nodes, cmp);
List<Integer> currColumn = new ArrayList<>();
int colIndex = nodes.get(0)[0];
for (int[] arr : nodes) {
int col = arr[0], row = arr[1], val = arr[2];
if (col == colIndex) {
currColumn.add(val);
} else {
result.add(currColumn);
colIndex = col;
currColumn = new ArrayList<>();
currColumn.add(val);
}
}
result.add(currColumn);
return result;
}
private void DFS(TreeNode curr, int row, int col, List<int[]> nodes) {
int[] arr = new int[3];
arr[0] = col;
arr[1] = row;
arr[2] = curr.val;
nodes.add(arr);
if (curr.left != null) DFS(curr.left, row + 1, col - 1, nodes);
if (curr.right != null) DFS(curr.right, row + 1, col + 1, nodes);
}
}