Skip to content

Commit 86f076a

Browse files
committed
feat: add solutions to lc problem: No.0987
No.0987.Vertical Order Traversal of a Binary Tree
1 parent 2bfcb9f commit 86f076a

File tree

9 files changed

+144
-11
lines changed

9 files changed

+144
-11
lines changed

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md

+59-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
<li><code>0 <= Node.val <= 1000</code></li>
6161
</ul>
6262

63-
6463
## 解法
6564

6665
<!-- 这里可写通用的实现逻辑 -->
@@ -72,15 +71,72 @@
7271
<!-- 这里可写当前语言的特殊实现逻辑 -->
7372

7473
```python
75-
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, val=0, left=None, right=None):
77+
# self.val = val
78+
# self.left = left
79+
# self.right = right
80+
class Solution:
81+
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
82+
def dfs(root, i, j):
83+
if root is None:
84+
return
85+
nodes.append((i, j, root.val))
86+
dfs(root.left, i + 1, j - 1)
87+
dfs(root.right, i + 1, j + 1)
88+
89+
nodes = []
90+
dfs(root, 0, 0)
91+
nodes.sort(key=lambda x: (x[1], x[0], x[2]))
92+
ans = []
93+
prev = -2000
94+
for i, j, v in nodes:
95+
if prev != j:
96+
ans.append([])
97+
prev = j
98+
ans[-1].append(v)
99+
return ans
76100
```
77101

78102
### **Java**
79103

80104
<!-- 这里可写当前语言的特殊实现逻辑 -->
81105

82106
```java
107+
class Solution {
108+
public List<List<Integer>> verticalTraversal(TreeNode root) {
109+
List<int[]> list = new ArrayList<>();
110+
dfs(root, 0, 0, list);
111+
list.sort(new Comparator<int[]>() {
112+
@Override
113+
public int compare(int[] o1, int[] o2) {
114+
if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
115+
if (o1[1] != o2[1]) return Integer.compare(o2[1], o1[1]);
116+
return Integer.compare(o1[2], o2[2]);
117+
}
118+
});
119+
List<List<Integer>> res = new ArrayList<>();
120+
int preX = 1;
121+
for (int[] cur : list) {
122+
if (preX != cur[0]) {
123+
res.add(new ArrayList<>());
124+
preX = cur[0];
125+
}
126+
res.get(res.size() - 1).add(cur[2]);
127+
}
128+
return res;
129+
}
83130

131+
private void dfs(TreeNode root, int x, int y, List<int[]> list) {
132+
if (root == null) {
133+
return;
134+
}
135+
list.add(new int[]{x, y, root.val});
136+
dfs(root.left, x - 1, y - 1, list);
137+
dfs(root.right, x + 1, y - 1, list);
138+
}
139+
}
84140
```
85141

86142
### **TypeScript**
@@ -100,12 +156,11 @@
100156
* }
101157
*/
102158

103-
function verticalTraversal(root: TreeNode | null): number[][] {
159+
function verticalTraversal(root: TreeNode | null): number[][] {
104160
let solution = [];
105161
dfs(root, 0, 0, solution);
106162
// 优先依据i=2排序, 然后依据i=1排序
107163
solution.sort(compare);
108-
// console.log(solution);
109164
let ans = [];
110165
let pre = Number.MIN_SAFE_INTEGER;
111166
for (let node of solution) {

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md

+59-5
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,77 @@ Note that the solution remains the same since 5 and 6 are in the same location a
5757
<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
5858
</ul>
5959

60-
6160
## Solutions
6261

6362
<!-- tabs:start -->
6463

6564
### **Python3**
6665

6766
```python
68-
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
75+
def dfs(root, i, j):
76+
if root is None:
77+
return
78+
nodes.append((i, j, root.val))
79+
dfs(root.left, i + 1, j - 1)
80+
dfs(root.right, i + 1, j + 1)
81+
82+
nodes = []
83+
dfs(root, 0, 0)
84+
nodes.sort(key=lambda x: (x[1], x[0], x[2]))
85+
ans = []
86+
prev = -2000
87+
for i, j, v in nodes:
88+
if prev != j:
89+
ans.append([])
90+
prev = j
91+
ans[-1].append(v)
92+
return ans
6993
```
7094

7195
### **Java**
7296

7397
```java
98+
class Solution {
99+
public List<List<Integer>> verticalTraversal(TreeNode root) {
100+
List<int[]> list = new ArrayList<>();
101+
dfs(root, 0, 0, list);
102+
list.sort(new Comparator<int[]>() {
103+
@Override
104+
public int compare(int[] o1, int[] o2) {
105+
if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
106+
if (o1[1] != o2[1]) return Integer.compare(o2[1], o1[1]);
107+
return Integer.compare(o1[2], o2[2]);
108+
}
109+
});
110+
List<List<Integer>> res = new ArrayList<>();
111+
int preX = 1;
112+
for (int[] cur : list) {
113+
if (preX != cur[0]) {
114+
res.add(new ArrayList<>());
115+
preX = cur[0];
116+
}
117+
res.get(res.size() - 1).add(cur[2]);
118+
}
119+
return res;
120+
}
74121

122+
private void dfs(TreeNode root, int x, int y, List<int[]> list) {
123+
if (root == null) {
124+
return;
125+
}
126+
list.add(new int[]{x, y, root.val});
127+
dfs(root.left, x - 1, y - 1, list);
128+
dfs(root.right, x + 1, y - 1, list);
129+
}
130+
}
75131
```
76132

77133
### **TypeScript**
@@ -91,12 +147,10 @@ Note that the solution remains the same since 5 and 6 are in the same location a
91147
* }
92148
*/
93149

94-
function verticalTraversal(root: TreeNode | null): number[][] {
150+
function verticalTraversal(root: TreeNode | null): number[][] {
95151
let solution = [];
96152
dfs(root, 0, 0, solution);
97-
// 优先依据i=2排序, 然后依据i=1排序
98153
solution.sort(compare);
99-
// console.log(solution);
100154
let ans = [];
101155
let pre = Number.MIN_SAFE_INTEGER;
102156
for (let node of solution) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
9+
def dfs(root, i, j):
10+
if root is None:
11+
return
12+
nodes.append((i, j, root.val))
13+
dfs(root.left, i + 1, j - 1)
14+
dfs(root.right, i + 1, j + 1)
15+
16+
nodes = []
17+
dfs(root, 0, 0)
18+
nodes.sort(key=lambda x: (x[1], x[0], x[2]))
19+
ans = []
20+
prev = -2000
21+
for i, j, v in nodes:
22+
if prev != j:
23+
ans.append([])
24+
prev = j
25+
ans[-1].append(v)
26+
return ans

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/Solution.ts

-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
function verticalTraversal(root: TreeNode | null): number[][] {
1616
let solution = [];
1717
dfs(root, 0, 0, solution);
18-
// 优先依据i=2排序, 然后依据i=1排序
1918
solution.sort(compare);
20-
// console.log(solution);
2119
let ans = [];
2220
let pre = Number.MIN_SAFE_INTEGER;
2321
for (let node of solution) {
Binary file not shown.
Loading
Loading
Loading

0 commit comments

Comments
 (0)