Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b308a76

Browse files
Merge pull request SharingSource#604 from SharingSource/ac_oier
✨feat: add 987 814
2 parents f7c4659 + 2a7b3b7 commit b308a76

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

‎Index/DFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
| [736. Lisp 语法解析](https://leetcode.cn/problems/parse-lisp-expression/) | [LeetCode 题解链接](https://leetcode.cn/problems/parse-lisp-expression/solution/by-ac_oier-i7w1/) | 困难 | 🤩🤩🤩🤩 |
3939
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
4040
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
41+
| [814. 二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/) | [LeetCode 题解链接](https://leetcode.cn/problems/binary-tree-pruning/solution/by-ac_oier-7me9/) | 中等 | 🤩🤩🤩🤩🤩 |
4142
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
4243
| [872. 叶子相似的树](https://leetcode-cn.com/problems/leaf-similar-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/leaf-similar-trees/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-udfc/) | 简单 | 🤩🤩🤩 |
4344
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |

‎Index/二叉树.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/construct-string-from-binary-tree/solution/by-ac_oier-i2sk/) | 简单 | 🤩🤩🤩🤩 |
1111
| [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 |
1212
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
13+
| [814. 二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/) | [LeetCode 题解链接](https://leetcode.cn/problems/binary-tree-pruning/solution/by-ac_oier-7me9/) | 中等 | 🤩🤩🤩🤩🤩 |
1314
| [863. 二叉树中所有距离为 K 的结点](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-x6hak/) | 中等 | 🤩🤩🤩🤩 |
1415
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
1516
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[814. 二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/solution/by-ac_oier-7me9/)** ,难度为 **中等**
4+
5+
Tag : 「二叉树」、「DFS」、「递归」
6+
7+
8+
9+
给你二叉树的根结点 `root`,此外树的每个结点的值要么是 0ドル$ ,要么是 1ドル$ 。
10+
11+
返回移除了所有不包含 1ドル$ 的子树的原二叉树。
12+
13+
节点 `node` 的子树为 `node` 本身加上所有 `node` 的后代。
14+
15+
示例 1:
16+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png)
17+
```
18+
输入:root = [1,null,0,0,1]
19+
20+
输出:[1,null,0,null,1]
21+
22+
解释:
23+
只有红色节点满足条件"所有不包含 1 的子树"。 右图为返回的答案。
24+
```
25+
示例 2:
26+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png)
27+
```
28+
输入:root = [1,0,1,0,0,0,1]
29+
30+
输出:[1,null,1,null,1]
31+
```
32+
示例 3:
33+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png)
34+
```
35+
输入:root = [1,1,0,1,1,0,1,0]
36+
37+
输出:[1,1,0,1,1,null,1]
38+
```
39+
40+
提示:
41+
* 树中节点的数目在范围 $[1, 200]$ 内
42+
* `Node.val` 为 0ドル$ 或 1ドル$
43+
44+
---
45+
46+
### 递归
47+
48+
根据题意,我们将原函数 `pruneTree` 作为递归函数,递归函数的含义为「将入参 `root` 中的所有不包含 1ドル$ 的子树移除,并返回新树头结点」。
49+
50+
不失一般性的考虑任意节点作为入参该如何处理:我们可以递归处理左右子树,并将新左右子树重新赋值给 `root`。由于当前节点 `root` 的左右子树可能为空树,因此我们要增加递归函数入参为空的边界处理。
51+
52+
当递归操作完成后,若左右节点任一值不为空(说明当前节点 `root` 不为叶子节点),我们可以直接返回 `root`,否则根据 `root` 的值是否为 0ドル$ 来决定返回空树还是 `root` 本身。
53+
54+
Java 代码:
55+
```Java
56+
class Solution {
57+
public TreeNode pruneTree(TreeNode root) {
58+
if (root == null) return null;
59+
root.left = pruneTree(root.left);
60+
root.right = pruneTree(root.right);
61+
if (root.left != null || root.right != null) return root;
62+
return root.val == 0 ? null : root;
63+
}
64+
}
65+
```
66+
TypeScript 代码:
67+
```TypeScript
68+
function pruneTree(root: TreeNode | null): TreeNode | null {
69+
if (root == null) return null
70+
root.left = pruneTree(root.left)
71+
root.right = pruneTree(root.right)
72+
if (root.left != null || root.right != null) return root
73+
return root.val == 0 ? null : root
74+
};
75+
```
76+
* 时间复杂度:$O(n)$
77+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
78+
79+
---
80+
81+
### 最后
82+
83+
这是我们「刷穿 LeetCode」系列文章的第 `No.814` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
84+
85+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
86+
87+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
88+
89+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
90+

‎LeetCode/981-990/987. 二叉树的垂序遍历(困难).md‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class Solution {
113113
* 时间复杂度:令总节点数量为 $n,ドル填充哈希表时进行树的遍历,复杂度为 $O(n)$;构造答案时需要进行排序,复杂度为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$
114114
* 空间复杂度:$O(n)$
115115

116-
117116
---
118117

119118
### DFS + 优先队列(堆)
@@ -160,6 +159,84 @@ class Solution {
160159

161160
---
162161

162+
### DFS + 哈希嵌套 + 排序
163+
164+
当然,如果想锻炼一下自己的代码能力,不使用三元组 $(col, row, val)$ 进行存储,而是使用哈希表嵌套,也是可以的。
165+
166+
用三个「哈希表」来记录相关信息:
167+
168+
1. 使用 `node2row``node2col` 分别用来记录「**节点到行**」&「**节点到列**」的映射关系,并实现 `dfs1` 对树进行遍历,目的是为了记录下相关的映射关系;
169+
170+
2. 使用 `col2row2nodes` 记录「**从列到行,从行到节点集**」的映射关系,具体的存储格式为 `{col : {row : [node1, node2, ... ]}}`,实现 `dfs2` 再次进行树的遍历,配合之前 `node2row``node2col`信息,填充 `col2row2nodes` 的映射关系;
171+
172+
3. 按照题意,按「列号从小到大」,对于同列节点,按照「行号从小到大」,对于同列同行元素,按照「节点值从小到大」的规则,使用 `col2row2nodes` + 排序 构造答案。
173+
174+
> 注意:本解法可以只进行一次树的遍历,分两步主要是不想 `dfs` 操作过于复杂,加大读者的阅读难度,于是在拆开不影响复杂度上界的情况,选择了分两步。
175+
176+
代码:
177+
```Java
178+
class Solution {
179+
Map<TreeNode, Integer> node2col = new HashMap<>(), node2row = new HashMap<>();
180+
Map<Integer, Map<Integer, List<Integer>>> col2row2nodes = new HashMap<>();
181+
public List<List<Integer>> verticalTraversal(TreeNode root) {
182+
List<List<Integer>> ans = new ArrayList<>();
183+
node2col.put(root, 0);
184+
node2row.put(root, 0);
185+
dfs1(root);
186+
dfs2(root);
187+
List<Integer> cols = new ArrayList<>(col2row2nodes.keySet());
188+
Collections.sort(cols);
189+
for (int col : cols) {
190+
Map<Integer, List<Integer>> row2nodes = col2row2nodes.get(col);
191+
List<Integer> rows = new ArrayList<>(row2nodes.keySet());
192+
Collections.sort(rows);
193+
List<Integer> cur = new ArrayList<>();
194+
for (int row : rows) {
195+
List<Integer> nodes = row2nodes.get(row);
196+
Collections.sort(nodes);
197+
cur.addAll(nodes);
198+
}
199+
ans.add(cur);
200+
}
201+
return ans;
202+
}
203+
// 树的遍历,根据「节点到列」&「节点到行」的映射关系,构造出「从列到行,从行到节点集」的映射关系
204+
void dfs2(TreeNode root) {
205+
if (root == null) return ;
206+
int col = node2col.get(root), row = node2row.get(root);
207+
Map<Integer, List<Integer>> row2nodes = col2row2nodes.getOrDefault(col, new HashMap<>());
208+
List<Integer> nodes = row2nodes.getOrDefault(row, new ArrayList<>());
209+
nodes.add(root.val);
210+
row2nodes.put(row, nodes);
211+
col2row2nodes.put(col, row2nodes);
212+
dfs2(root.left);
213+
dfs2(root.right);
214+
}
215+
// 树的遍历,记录下「节点到列」&「节点到行」的映射关系
216+
void dfs1(TreeNode root) {
217+
if (root == null) return ;
218+
if (root.left != null) {
219+
int col = node2col.get(root);
220+
node2col.put(root.left, col - 1);
221+
int row = node2row.get(root);
222+
node2row.put(root.left, row + 1);
223+
dfs1(root.left);
224+
}
225+
if (root.right != null) {
226+
int col = node2col.get(root);
227+
node2col.put(root.right, col + 1);
228+
int row = node2row.get(root);
229+
node2row.put(root.right, row + 1);
230+
dfs1(root.right);
231+
}
232+
}
233+
}
234+
```
235+
* 时间复杂度:令总的节点数量为 $n,ドル填充几个哈希表的复杂度为 $O(n)$;构造答案时需要对行号、列号和节点值进行排序,总的复杂度上界为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$
236+
* 空间复杂度:$O(n)$
237+
238+
---
239+
163240
### 最后
164241

165242
这是我们「刷穿 LeetCode」系列文章的第 `No.987` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /