Skip to content

Commit 936a5ac

Browse files
committed
✨feat: add 669
1 parent 99b45c6 commit 936a5ac

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Index/BFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [589. N 叉树的前序遍历](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/gong-shui-san-xie-shu-de-sou-suo-yun-yon-pse1/) | 简单 | 🤩🤩🤩 |
1212
| [590. N 叉树的后序遍历](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/) | 简单 | 🤩🤩🤩 |
1313
| [623. 在二叉树中增加一行](https://leetcode.cn/problems/add-one-row-to-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/add-one-row-to-tree/solution/by-ac_oier-sc34/) | 中等 | 🤩🤩🤩🤩 |
14+
| [669. 修剪二叉搜索树](https://leetcode.cn/problems/trim-a-binary-search-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/trim-a-binary-search-tree/solution/by-ac_oier-help/) | 中等 | 🤩🤩🤩🤩 |
1415
| [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 |
1516
| [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/) | 困难 | 🤩🤩🤩 |
1617
| [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/) | 简单 | 🤩🤩🤩 |

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
| [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 |
3737
| [655. 输出二叉树](https://leetcode.cn/problems/print-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/print-binary-tree/solution/by-ac_oier-mays/) | 中等 | 🤩🤩🤩🤩 |
3838
| [662. 二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/by-ac_oier-33er/) | 中等 | 🤩🤩🤩🤩 |
39+
| [669. 修剪二叉搜索树](https://leetcode.cn/problems/trim-a-binary-search-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/trim-a-binary-search-tree/solution/by-ac_oier-help/) | 中等 | 🤩🤩🤩🤩 |
3940
| [676. 实现一个魔法字典](https://leetcode.cn/problems/implement-magic-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/implement-magic-dictionary/solution/by-ac_oier-a01l/) | 中等 | 🤩🤩🤩🤩🤩 |
4041
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
4142
| [687. 最长同值路径](https://leetcode.cn/problems/longest-univalue-path/) | [LeetCode 题解链接](https://leetcode.cn/problems/longest-univalue-path/solution/by-ac_oier-8ue8/) | 中等 | 🤩🤩🤩🤩 |

Index/二叉树.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 |
1515
| [655. 输出二叉树](https://leetcode.cn/problems/print-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/print-binary-tree/solution/by-ac_oier-mays/) | 中等 | 🤩🤩🤩🤩 |
1616
| [662. 二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/by-ac_oier-33er/) | 中等 | 🤩🤩🤩🤩🤩 |
17+
| [669. 修剪二叉搜索树](https://leetcode.cn/problems/trim-a-binary-search-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/trim-a-binary-search-tree/solution/by-ac_oier-help/) | 中等 | 🤩🤩🤩🤩 |
1718
| [687. 最长同值路径](https://leetcode.cn/problems/longest-univalue-path/) | [LeetCode 题解链接](https://leetcode.cn/problems/longest-univalue-path/solution/by-ac_oier-8ue8/) | 中等 | 🤩🤩🤩🤩 |
1819
| [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/) | 简单 | 🤩🤩🤩 |
1920
| [814. 二叉树剪枝](https://leetcode.cn/problems/binary-tree-pruning/) | [LeetCode 题解链接](https://leetcode.cn/problems/binary-tree-pruning/solution/by-ac_oier-7me9/) | 中等 | 🤩🤩🤩🤩🤩 |
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[669. 修剪二叉搜索树](https://leetcode.cn/problems/trim-a-binary-search-tree/solution/by-ac_oier-help/)** ,难度为 **中等**
4+
5+
Tag : 「BST」、「二叉树」、「递归」、「迭代」
6+
7+
8+
9+
给你二叉搜索树的根节点 `root`,同时给定最小边界 `low` 和最大边界 `high`。通过修剪二叉搜索树,使得所有节点的值在 $[low, high]$ 中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
10+
11+
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
12+
13+
示例 1:
14+
![](https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg)
15+
```
16+
输入:root = [1,0,2], low = 1, high = 2
17+
18+
输出:[1,null,2]
19+
```
20+
示例 2:
21+
![](https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg)
22+
```
23+
输入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
24+
25+
输出:[3,2,null,1]
26+
```
27+
28+
提示:
29+
* 树中节点数在范围 $[1, 10^4]$ 内
30+
* $0 <= Node.val <= 10^4$
31+
* 树中每个节点的值都是 唯一 的
32+
* 题目数据保证输入是一棵有效的二叉搜索树
33+
* $0 <= low <= high <= 10^4$
34+
35+
---
36+
37+
### 递归
38+
39+
由于被修剪的是二叉搜索树,因此修剪过程必然能够顺利进行。
40+
41+
容易想到使用原函数作为递归函数:
42+
43+
*`root.val` 小于边界值 `low`,则 `root` 的左子树必然均小于边界值,我们递归处理 `root.right` 即可;
44+
*`root.val` 大于边界值 `high`,则 `root` 的右子树必然均大于边界值,我们递归处理 `root.left` 即可;
45+
*`root.val` 符合要求,则 `root` 可被保留,递归处理其左右节点并重新赋值即可。
46+
47+
Java 代码:
48+
```Java
49+
class Solution {
50+
public TreeNode trimBST(TreeNode root, int low, int high) {
51+
if (root == null) return null;
52+
if (root.val < low) return trimBST(root.right, low, high);
53+
else if (root.val > high) return trimBST(root.left, low, high);
54+
root.left = trimBST(root.left, low, high);
55+
root.right = trimBST(root.right, low, high);
56+
return root;
57+
}
58+
}
59+
```
60+
TypeScript 代码:
61+
```TypeScript
62+
function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | null {
63+
if (root == null) return null
64+
if (root.val < low) return trimBST(root.right, low, high)
65+
else if (root.val > high) return trimBST(root.left, low, high)
66+
root.left = trimBST(root.left, low, high)
67+
root.right = trimBST(root.right, low, high)
68+
return root
69+
};
70+
```
71+
* 时间复杂度:$O(n)$
72+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
73+
74+
---
75+
76+
### 迭代
77+
78+
自然能够使用「迭代」进行求解:起始先从给定的 `root` 进行出发,找到第一个满足值符合 $[low, high]$ 范围的节点,该节点为最后要返回的根节点 `ans`
79+
80+
随后考虑如何修剪 `ans` 的左右节点:当根节点符合 $[low, high]$ 要求时,修剪左右节点过程中仅需考虑一边的边界值即可。即对于 `ans.left` 只需考虑将值小于 `low` 的节点去掉(因为二叉搜索树的特性,`ans` 满足不大于 `high` 要求,则其左节点必然满足);同理 `ans.right` 只需要考虑将大于 `high` 的节点去掉即可。
81+
82+
Java 代码:
83+
```Java
84+
class Solution {
85+
public TreeNode trimBST(TreeNode root, int low, int high) {
86+
while (root != null && (root.val < low || root.val > high)) root = root.val < low ? root.right : root.left;
87+
TreeNode ans = root;
88+
while (root != null) {
89+
while (root.left != null && root.left.val < low) root.left = root.left.right;
90+
root = root.left;
91+
}
92+
root = ans;
93+
while (root != null) {
94+
while (root.right != null && root.right.val > high) root.right = root.right.left;
95+
root = root.right;
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
TypeScript 代码:
102+
```TypeScript
103+
function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | null {
104+
while (root != null && (root.val < low || root.val > high)) root = root.val < low ? root.right : root.left
105+
const ans = root
106+
while (root != null) {
107+
while (root.left != null && root.left.val < low) root.left = root.left.right
108+
root = root.left
109+
}
110+
root = ans
111+
while (root != null) {
112+
while (root.right != null && root.right.val > high) root.right = root.right.left
113+
root = root.right
114+
}
115+
return ans
116+
};
117+
```
118+
* 时间复杂度:$O(n)$
119+
* 空间复杂度:$O(1)$
120+
121+
---
122+
123+
### 最后
124+
125+
这是我们「刷穿 LeetCode」系列文章的第 `No.669` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
126+
127+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
128+
129+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
130+
131+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
132+

0 commit comments

Comments
 (0)