-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_987.java
107 lines (100 loc) · 3.89 KB
/
_987.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
package com.fishercoder.solutions.firstthousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.*;
public class _987 {
public static class Solution1 {
/*
* credit: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/231148/Java-TreeMap-Solution
*/
public List<List<Integer>> verticalTraversal(TreeNode root) {
TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map = new TreeMap<>();
dfs(root, 0, 0, map);
List<List<Integer>> list = new ArrayList<>();
for (TreeMap<Integer, PriorityQueue<Integer>> yMap : map.values()) {
list.add(new ArrayList<>());
for (PriorityQueue<Integer> nodes : yMap.values()) {
while (!nodes.isEmpty()) {
list.get(list.size() - 1).add(nodes.poll());
}
}
}
return list;
}
private void dfs(
TreeNode root,
int x,
int y,
TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map) {
if (root == null) {
return;
}
if (!map.containsKey(x)) {
map.put(x, new TreeMap<>());
}
if (!map.get(x).containsKey(y)) {
map.get(x).put(y, new PriorityQueue<>());
}
map.get(x).get(y).offer(root.val);
dfs(root.left, x - 1, y + 1, map);
dfs(root.right, x + 1, y + 1, map);
}
}
public static class Solution2 {
/*
* My completely original solution on 6/13/2024.
*/
public List<List<Integer>> verticalTraversal(TreeNode root) {
TreeMap<Integer, List<NodeWithCoords>> map = new TreeMap<>();
Queue<NodeWithCoords> q = new LinkedList<>();
q.offer(new NodeWithCoords(root, 0, 0));
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
NodeWithCoords curr = q.poll();
int col = curr.col;
int row = curr.row;
List<NodeWithCoords> list = map.getOrDefault(col, new ArrayList<>());
list.add(curr);
map.put(col, list);
if (curr.node.left != null) {
q.offer(new NodeWithCoords(curr.node.left, row + 1, col - 1));
}
if (curr.node.right != null) {
q.offer(new NodeWithCoords(curr.node.right, row + 1, col + 1));
}
}
}
List<List<Integer>> result = new ArrayList<>();
for (Integer key : map.keySet()) {
List<NodeWithCoords> list = map.get(key);
Collections.sort(
list,
(a, b) -> {
if (a.row != b.row) {
return a.row - b.row;
} else if (a.col != b.col) {
return a.col - b.col;
} else {
return a.node.val - b.node.val;
}
});
List<Integer> intList = new ArrayList<>();
for (NodeWithCoords nodeWithCoords : list) {
intList.add(nodeWithCoords.node.val);
}
result.add(intList);
}
return result;
}
class NodeWithCoords {
TreeNode node;
int row;
int col;
public NodeWithCoords(TreeNode node, int row, int col) {
this.node = node;
this.row = row;
this.col = col;
}
}
}
}