Skip to content

Commit ee74fa9

Browse files
add a solution for 987
1 parent f0bbcb3 commit ee74fa9

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_987.java

+61-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
import java.util.List;
6-
import java.util.ArrayList;
7-
import java.util.PriorityQueue;
8-
import java.util.TreeMap;
5+
import java.util.*;
96

107
public class _987 {
118
public static class Solution1 {
@@ -42,4 +39,64 @@ private void dfs(TreeNode root, int x, int y, TreeMap<Integer, TreeMap<Integer,
4239
dfs(root.right, x + 1, y + 1, map);
4340
}
4441
}
42+
43+
public static class Solution2 {
44+
45+
/**
46+
* My completely original solution on 6/13/2024.
47+
*/
48+
public List<List<Integer>> verticalTraversal(TreeNode root) {
49+
TreeMap<Integer, List<NodeWithCoords>> map = new TreeMap<>();
50+
Queue<NodeWithCoords> q = new LinkedList<>();
51+
q.offer(new NodeWithCoords(root, 0, 0));
52+
while (!q.isEmpty()) {
53+
int size = q.size();
54+
for (int i = 0; i < size; i++) {
55+
NodeWithCoords curr = q.poll();
56+
int col = curr.col;
57+
int row = curr.row;
58+
List<NodeWithCoords> list = map.getOrDefault(col, new ArrayList<>());
59+
list.add(curr);
60+
map.put(col, list);
61+
if (curr.node.left != null) {
62+
q.offer(new NodeWithCoords(curr.node.left, row + 1, col - 1));
63+
}
64+
if (curr.node.right != null) {
65+
q.offer(new NodeWithCoords(curr.node.right, row + 1, col + 1));
66+
}
67+
}
68+
}
69+
List<List<Integer>> result = new ArrayList<>();
70+
for (Integer key : map.keySet()) {
71+
List<NodeWithCoords> list = map.get(key);
72+
Collections.sort(list, (a, b) -> {
73+
if (a.row != b.row) {
74+
return a.row - b.row;
75+
} else if (a.col != b.col) {
76+
return a.col - b.col;
77+
} else {
78+
return a.node.val - b.node.val;
79+
}
80+
});
81+
List<Integer> intList = new ArrayList<>();
82+
for (NodeWithCoords nodeWithCoords : list) {
83+
intList.add(nodeWithCoords.node.val);
84+
}
85+
result.add(intList);
86+
}
87+
return result;
88+
}
89+
90+
class NodeWithCoords {
91+
TreeNode node;
92+
int row;
93+
int col;
94+
95+
public NodeWithCoords(TreeNode node, int row, int col) {
96+
this.node = node;
97+
this.row = row;
98+
this.col = col;
99+
}
100+
}
101+
}
45102
}

Diff for: src/test/java/com/fishercoder/_987Test.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._987;
6-
import org.junit.BeforeClass;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

99
import java.util.Arrays;
1010
import java.util.List;
1111

12-
import static junit.framework.Assert.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
1313

1414
public class _987Test {
1515
private static _987.Solution1 solution1;
16+
private static _987.Solution2 solution2;
1617
private static TreeNode root;
1718
private static List<List<Integer>> expected;
1819
private static List<List<Integer>> actual;
1920

20-
@BeforeClass
21-
public static void setup() {
21+
@BeforeEach
22+
public void setup() {
2223
solution1 = new _987.Solution1();
24+
solution2 = new _987.Solution2();
2325
}
2426

2527
@Test
@@ -28,6 +30,8 @@ public void test1() {
2830
expected = Arrays.asList(Arrays.asList(9), Arrays.asList(3, 15), Arrays.asList(20), Arrays.asList(7));
2931
actual = solution1.verticalTraversal(root);
3032
assertEquals(expected, actual);
33+
actual = solution2.verticalTraversal(root);
34+
assertEquals(expected, actual);
3135
}
3236

3337
@Test

0 commit comments

Comments
 (0)