Skip to content

Commit 59842a5

Browse files
committed
添加了问题“987.二叉树的垂序遍历”的代码和题解
1 parent 6f33a8b commit 59842a5

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-02-13 11:27:11
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-02-13 11:34:26
6+
'''
7+
from typing import List
8+
9+
# Definition for a binary tree node.
10+
class TreeNode:
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
16+
class Solution:
17+
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
18+
q = [(0, 0, root)] # [(col, depth, node), ...
19+
v = [] # [(col, depth, val), ...]
20+
while q:
21+
thisCol, thisDepth, thisNode = q.pop() # actually is't queue but a stack
22+
v.append((thisCol, thisDepth, thisNode.val))
23+
if thisNode.left:
24+
q.append((thisCol - 1, thisDepth + 1, thisNode.left))
25+
if thisNode.right:
26+
q.append((thisCol + 1, thisDepth + 1, thisNode.right))
27+
v.sort()
28+
ans = []
29+
lastCol = 100000
30+
for col, _, val in v:
31+
if col != lastCol:
32+
lastCol = col
33+
ans.append([])
34+
ans[-1].append(val)
35+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
|0961.在长度2N的数组中找出重复N次的元素|简单|<a href="https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/05/21/LeetCode%200961.%E5%9C%A8%E9%95%BF%E5%BA%A62N%E7%9A%84%E6%95%B0%E7%BB%84%E4%B8%AD%E6%89%BE%E5%87%BA%E9%87%8D%E5%A4%8DN%E6%AC%A1%E7%9A%84%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/124897591" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-closest-palindrome/solution/letmefly-da-an-hou-xuan-by-letmefly-2-k2vn/" target="_blank">LeetCode题解</a>|
273273
|0970.强整数|中等|<a href="https://leetcode.cn/problems/powerful-integers/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/05/02/LeetCode%200970.%E5%BC%BA%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130461751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/powerful-integers/solutions/2253101/letmefly-970qiang-zheng-shu-by-tisfy-w22m/" target="_blank">LeetCode题解</a>|
274274
|0982.按位与为零的三元组|困难|<a href="https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/03/04/LeetCode%200982.%E6%8C%89%E4%BD%8D%E4%B8%8E%E4%B8%BA%E9%9B%B6%E7%9A%84%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129334313" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/solutions/2146568/letmefly-982an-wei-yu-wei-ling-de-san-yu-071t/" target="_blank">LeetCode题解</a>|
275+
|0987.二叉树的垂序遍历|困难|<a href="https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/02/13/LeetCode%200987.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%9E%82%E5%BA%8F%E9%81%8D%E5%8E%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136106019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/solutions/2639036/letmefly-987er-cha-shu-de-chui-xu-bian-l-l9yo/" target="_blank">LeetCode题解</a>|
275276
|0993.二叉树的堂兄弟节点|简单|<a href="https://leetcode.cn/problems/cousins-in-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/02/08/LeetCode%200993.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%A0%82%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136078040" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/cousins-in-binary-tree/solutions/2635757/letmefly-993er-cha-shu-de-tang-xiong-di-eyvq6/" target="_blank">LeetCode题解</a>|
276277
|1003.检查替换后的词是否有效|中等|<a href="https://leetcode.cn/problems/check-if-word-is-valid-after-substitutions/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/05/03/LeetCode%201003.%E6%A3%80%E6%9F%A5%E6%9B%BF%E6%8D%A2%E5%90%8E%E7%9A%84%E8%AF%8D%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130470201" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-word-is-valid-after-substitutions/solutions/2254002/letmefly-1003jian-cha-ti-huan-hou-de-ci-we7vj/" target="_blank">LeetCode题解</a>|
277278
|1010.总持续时间可被60整除的歌曲|中等|<a href="https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/05/07/LeetCode%201010.%E6%80%BB%E6%8C%81%E7%BB%AD%E6%97%B6%E9%97%B4%E5%8F%AF%E8%A2%AB60%E6%95%B4%E9%99%A4%E7%9A%84%E6%AD%8C%E6%9B%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130544996" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/solutions/2260413/letmefly-1010zong-chi-xu-shi-jian-ke-bei-c8di/" target="_blank">LeetCode题解</a>|
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: 987.二叉树的垂序遍历
3+
date: 2024-02-13 11:03:40
4+
tags: [题解, LeetCode, 困难, 树, 深度优先搜索, 广度优先搜索, BFS, 哈希表, 二叉树]
5+
---
6+
7+
# 【LetMeFly】987.二叉树的垂序遍历:遍历时存节点信息,遍历完自定义排序
8+
9+
力扣题目链接:[https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/](https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/)
10+
11+
<p>给你二叉树的根结点 <code>root</code> ,请你设计算法计算二叉树的<em> </em><strong>垂序遍历</strong> 序列。</p>
12+
13+
<p>对位于 <code>(row, col)</code> 的每个结点而言,其左右子结点分别位于 <code>(row + 1, col - 1)</code> 和 <code>(row + 1, col + 1)</code> 。树的根结点位于 <code>(0, 0)</code> 。</p>
14+
15+
<p>二叉树的 <strong>垂序遍历</strong> 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,则按结点的值从小到大进行排序。</p>
16+
17+
<p>返回二叉树的 <strong>垂序遍历</strong> 序列。</p>
18+
19+
<p> </p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg" style="width: 431px; height: 304px;" />
23+
<pre>
24+
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
25+
<strong>输出:</strong>[[9],[3,15],[20],[7]]
26+
<strong>解释:</strong>
27+
列 -1 :只有结点 9 在此列中。
28+
列 0 :只有结点 3 和 15 在此列中,按从上到下顺序。
29+
列 1 :只有结点 20 在此列中。
30+
列 2 :只有结点 7 在此列中。</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg" style="width: 512px; height: 304px;" />
34+
<pre>
35+
<strong>输入:</strong>root = [1,2,3,4,5,6,7]
36+
<strong>输出:</strong>[[4],[2],[1,5,6],[3],[7]]
37+
<strong>解释:</strong>
38+
列 -2 :只有结点 4 在此列中。
39+
列 -1 :只有结点 2 在此列中。
40+
列 0 :结点 1 、5 和 6 都在此列中。
41+
1 在上面,所以它出现在前面。
42+
5 和 6 位置都是 (2, 0) ,所以按值从小到大排序,5 在 6 的前面。
43+
列 1 :只有结点 3 在此列中。
44+
列 2 :只有结点 7 在此列中。
45+
</pre>
46+
47+
<p><strong>示例 3:</strong></p>
48+
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg" style="width: 512px; height: 304px;" />
49+
<pre>
50+
<strong>输入:</strong>root = [1,2,3,4,6,5,7]
51+
<strong>输出:</strong>[[4],[2],[1,5,6],[3],[7]]
52+
<strong>解释:</strong>
53+
这个示例实际上与示例 2 完全相同,只是结点 5 和 6 在树中的位置发生了交换。
54+
因为 5 和 6 的位置仍然相同,所以答案保持不变,仍然按值从小到大排序。</pre>
55+
56+
<p> </p>
57+
58+
<p><strong>提示:</strong></p>
59+
60+
<ul>
61+
<li>树中结点数目总数在范围 <code>[1, 1000]</code> 内</li>
62+
<li><code>0 <= Node.val <= 1000</code></li>
63+
</ul>
64+
65+
66+
67+
## 方法一:遍历时存节点信息,遍历完自定义排序(以广度优先搜索为例)
68+
69+
[广度优先搜索(BFS)](https://leetcode.letmefly.xyz/tags/BFS/)相信大家都不陌生,因此我们可以二话不说将二叉树广搜一遍,在搜索过程中将所需要的信息计算并存下来,剩下的就是按照题目规则自定义排序了。
70+
71+
都需要哪些信息呢?需要“节点在哪一列”、“节点深度”、“节点值”。
72+
73+
遍历过程中将上述三元组存下来,遍历完成后依据这三个信息排序,作为答案并返回即可。
74+
75+
+ 时间复杂度$O(N\log N)$,其中$N$是二叉树中节点的个数
76+
+ 空间复杂度$O(N)$
77+
78+
### AC代码
79+
80+
#### C++
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
vector<vector<int>> verticalTraversal(TreeNode* root) {
86+
queue<NodeInfo> q; // [<node, <col, height>>, ...
87+
q.push({root, {0, 1}});
88+
vector<NodeInfo> v;
89+
while (q.size()) {
90+
NodeInfo thisNode = q.front();
91+
q.pop();
92+
v.push_back(thisNode);
93+
if (thisNode.first->left) {
94+
q.push({thisNode.first->left, {thisNode.second.first - 1, thisNode.second.second + 1}});
95+
}
96+
if (thisNode.first->right) {
97+
q.push({thisNode.first->right, {thisNode.second.first + 1, thisNode.second.second + 1}});
98+
}
99+
}
100+
sort(v.begin(), v.end(), [&](const NodeInfo& a, const NodeInfo& b) {
101+
return a.second == b.second ? a.first->val < b.first->val : a.second < b.second;
102+
});
103+
vector<vector<int>> ans;
104+
int lastCol = 1000000;
105+
for (NodeInfo& a : v) {
106+
if (a.second.first != lastCol) {
107+
lastCol = a.second.first;
108+
ans.push_back({});
109+
}
110+
ans.back().push_back(a.first->val);
111+
}
112+
return ans;
113+
}
114+
};
115+
```
116+
117+
#### Python
118+
119+
```python
120+
# from typing import List
121+
122+
# # Definition for a binary tree node.
123+
# class TreeNode:
124+
# def __init__(self, val=0, left=None, right=None):
125+
# self.val = val
126+
# self.left = left
127+
# self.right = right
128+
129+
class Solution:
130+
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
131+
q = [(0, 0, root)] # [(col, depth, node), ...
132+
v = [] # [(col, depth, val), ...]
133+
while q:
134+
thisCol, thisDepth, thisNode = q.pop() # actually is't queue but a stack
135+
v.append((thisCol, thisDepth, thisNode.val))
136+
if thisNode.left:
137+
q.append((thisCol - 1, thisDepth + 1, thisNode.left))
138+
if thisNode.right:
139+
q.append((thisCol + 1, thisDepth + 1, thisNode.right))
140+
v.sort()
141+
ans = []
142+
lastCol = 100000
143+
for col, _, val in v:
144+
if col != lastCol:
145+
lastCol = col
146+
ans.append([])
147+
ans[-1].append(val)
148+
return ans
149+
150+
```
151+
152+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/02/13/LeetCode%200987.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%9E%82%E5%BA%8F%E9%81%8D%E5%8E%86/)哦~
153+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/136106019](https://letmefly.blog.csdn.net/article/details/136106019)

0 commit comments

Comments
 (0)