Skip to content

Commit b561254

Browse files
committed
✨feat: add 1038、1094、1423、2055、2246、2661
1 parent 0791119 commit b561254

17 files changed

+854
-42
lines changed

Index/二叉树.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
3535
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
3636
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
37+
| [1038. 从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/solutions/2552959/gong-shui-san-xie-bst-de-zhong-xu-bian-l-vtu1/) | 中等 | 🤩🤩🤩🤩 |
3738
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
3839
| [1305. 两棵二叉搜索树中的所有元素](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/solution/by-ac_oier-c8fv/) | 中等 | 🤩🤩🤩🤩 |
3940
| [剑指 Offer 04. 二维数组中的查找](https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/solution/by-ac_oier-7jo0/) | 中等 | 🤩🤩🤩🤩🤩 |
@@ -43,4 +44,3 @@
4344
| [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/by-ac_oier-3ehr/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 |
4546
| [面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/) | 中等 | 🤩🤩🤩🤩🤩 |
46-

Index/前缀和.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
2828
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
2929
| [1074. 元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/) | 困难 | 🤩🤩🤩 |
30+
| [1094. 拼车](https://leetcode.cn/problems/car-pooling/) | [LeetCode 题解链接](https://leetcode.cn/problems/car-pooling/solutions/2550276/gong-shui-san-xie-tu-jie-chai-fen-ru-men-opii/) | 中等 | 🤩🤩🤩🤩🤩 |
3031
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
3132
| [1208. 尽可能使字符串相等](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/solution/ni-bu-ke-neng-kan-bu-dong-de-qian-zhui-h-u4l1/) | 中等 | 🤩🤩🤩 |
3233
| [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 |

Index/哈希表.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
| [2034. 股票价格波动](https://leetcode-cn.com/problems/stock-price-fluctuation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-u6f4/) | 中等 | 🤩🤩🤩🤩 |
9696
| [2336. 无限集中的最小数字](https://leetcode.cn/problems/smallest-number-in-infinite-set/) | [LeetCode 题解链接](https://leetcode.cn/problems/smallest-number-in-infinite-set/solutions/2546157/gong-shui-san-xie-rong-yi-you-gao-xiao-d-431o/) | 中等 | 🤩🤩🤩🤩 |
9797
| [2342. 数位和相等数对的最大和](https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/solutions/2531511/gong-shui-san-xie-yong-bian-li-guo-cheng-kt3f/) | 中等 | 🤩🤩🤩🤩 |
98+
| [2661. 找出叠涂元素](https://leetcode.cn/problems/first-completely-painted-row-or-column/) | [LeetCode 题解链接](https://leetcode.cn/problems/first-completely-painted-row-or-column/solutions/2549034/gong-shui-san-xie-chang-gui-ha-xi-biao-y-jkxa/) | 中等 | 🤩🤩🤩🤩 |
9899
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
99100
| [面试题 17.10. 主要元素](https://leetcode-cn.com/problems/find-majority-element-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-majority-element-lcci/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-zkht/) | 简单 | 🤩🤩🤩🤩 |
100101
| [剑指 Offer 35. 复杂链表的复制](https://leetcode.cn/problems/fu-za-lian-biao-de-fu-zhi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/fu-za-lian-biao-de-fu-zhi-lcof/solution/by-ac_oier-6atv/) | 中等 | 🤩🤩🤩 |

Index/差分.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [798. 得分最高的最小轮调](https://leetcode-cn.com/problems/smallest-rotation-with-highest-score/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/smallest-rotation-with-highest-score/solution/gong-shui-san-xie-shang-xia-jie-fen-xi-c-p6kh/) | 困难 | 🤩🤩🤩🤩 |
44
| [995. K 连续位的最小翻转次数](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/po-su-tan-xin-jie-fa-yu-tan-xin-chai-fen-4lyy/) | 困难 | 🤩🤩🤩 |
5+
| [1094. 拼车](https://leetcode.cn/problems/car-pooling/) | [LeetCode 题解链接](https://leetcode.cn/problems/car-pooling/solutions/2550276/gong-shui-san-xie-tu-jie-chai-fen-ru-men-opii/) | 中等 | 🤩🤩🤩🤩🤩 |
56
| [1109. 航班预订统计](https://leetcode-cn.com/problems/corporate-flight-bookings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/corporate-flight-bookings/solution/gong-shui-san-xie-yi-ti-shuang-jie-chai-fm1ef/) | 中等 | 🤩🤩🤩🤩🤩 |
67
| [1450. 在既定时间做作业的学生人数](https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/solution/by-ac_oier-4ftz/) | 简单 | 🤩🤩🤩🤩🤩 |
78

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
| [2520. 统计能整除数字的位数](https://leetcode.cn/problems/count-the-digits-that-divide-a-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-the-digits-that-divide-a-number/solutions/2498966/gong-shui-san-xie-jian-dan-mo-ni-ti-shi-0ad2c/) | 简单 | 🤩🤩🤩🤩🤩 |
254254
| [2586. 统计范围内的元音字符串数](https://leetcode.cn/problems/count-the-number-of-vowel-strings-in-range/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-the-number-of-vowel-strings-in-range/solutions/2515898/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-di1t/) | 简单 | 🤩🤩🤩 |
255255
| [2609. 最长平衡子字符串](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/solutions/2517437/gong-shui-san-xie-on-shi-jian-o1-kong-ji-i8e7/) | 简单 | 🤩🤩🤩 |
256+
| [2661. 找出叠涂元素](https://leetcode.cn/problems/first-completely-painted-row-or-column/) | [LeetCode 题解链接](https://leetcode.cn/problems/first-completely-painted-row-or-column/solutions/2549034/gong-shui-san-xie-chang-gui-ha-xi-biao-y-jkxa/) | 中等 | 🤩🤩🤩🤩 |
256257
| [面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/) | 简单 | 🤩🤩🤩 |
257258
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
258259
| [面试题 01.08. 零矩阵](https://leetcode.cn/problems/zero-matrix-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/zero-matrix-lcci/solution/by-ac_oier-0lo0/) | 中等 | 🤩🤩🤩🤩 |

Index/滑动窗口.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
1919
| [1052. 爱生气的书店老板](https://leetcode-cn.com/problems/grumpy-bookstore-owner/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grumpy-bookstore-owner/solution/hua-dong-chuang-kou-luo-ti-by-ac_oier-nunu/) | 中等 | 🤩🤩🤩 |
2020
| [1208. 尽可能使字符串相等](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/solution/ni-bu-ke-neng-kan-bu-dong-de-qian-zhui-h-u4l1/) | 中等 | 🤩🤩🤩 |
21-
| [1423. 可获得的最大点数](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/solution/jian-dan-de-hua-dong-chuang-kou-he-kuai-1go5h/) | 中等 | 🤩🤩🤩🤩 |
21+
| [1423. 可获得的最大点数](https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/solutions/2551466/gong-shui-san-xie-zhuan-huan-wei-gu-ding-pbvd/) | 中等 | 🤩🤩🤩🤩 |
2222
| [1438. 绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/solution/xiang-jie-er-fen-hua-dong-chuang-kou-dan-41g1/) | 中等 | 🤩🤩🤩 |
2323
| [1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 |
2424
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1038. 从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/solutions/2552959/gong-shui-san-xie-bst-de-zhong-xu-bian-l-vtu1/)** ,难度为 **中等**
4+
5+
Tag : 「BST」、「中序遍历」
6+
7+
8+
9+
给定一个二叉搜索树 `root` (BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。
10+
11+
提醒一下, 二叉搜索树满足下列约束条件:
12+
13+
* 节点的左子树仅包含键小于节点键的节点。
14+
* 节点的右子树仅包含键大于节点键的节点。
15+
* 左右子树也必须是二叉搜索树。
16+
17+
示例 1:
18+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/05/03/tree.png)
19+
```
20+
输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
21+
22+
输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
23+
```
24+
示例 2:
25+
```
26+
输入:root = [0,null,1]
27+
28+
输出:[1,null,1]
29+
```
30+
31+
提示:
32+
* 树中的节点数在 $[1, 100]$ 范围内。
33+
* $0 <= Node.val <= 100$
34+
* 树中的所有值均不重复 。
35+
36+
---
37+
38+
### 中序遍历
39+
40+
利用 **`BST` 的中序遍历是有序** 的特性,我们可以通过两次遍历 `BST` 来求解问题。
41+
42+
首先,通过一次遍历,计算出整棵树的节点总和 `tot`,然后在中序遍历过程中,不断对 `tot` 进行更新,将其作为当前未遍历到的节点的总和,用于给当前节点赋值。
43+
44+
假设当前遍历到的节点为 `x`(起始节点值为 `t`),那么将节点更新为当前节点 `tot` 后,更新 `tot = tot - t`
45+
46+
这是常规的中序遍历做法,更进一步,如果将其中序遍历的顺序进行翻转(从「左中右」调整为「右中左」),则可实现一次遍历。
47+
48+
49+
Java 代码:
50+
```Java
51+
class Solution {
52+
int tot = 0;
53+
public TreeNode bstToGst(TreeNode root) {
54+
dfs(root);
55+
return root;
56+
}
57+
void dfs(TreeNode root) {
58+
if (root == null) return ;
59+
dfs(root.right);
60+
tot += root.val;
61+
root.val = tot;
62+
dfs(root.left);
63+
}
64+
}
65+
```
66+
C++ 代码:
67+
```C++
68+
class Solution {
69+
public:
70+
int tot = 0;
71+
TreeNode* bstToGst(TreeNode* root) {
72+
dfs(root);
73+
return root;
74+
}
75+
void dfs(TreeNode* root) {
76+
if (root == nullptr) return;
77+
dfs(root->right);
78+
tot += root->val;
79+
root->val = tot;
80+
dfs(root->left);
81+
}
82+
};
83+
```
84+
Python 代码:
85+
```Python
86+
class Solution:
87+
def bstToGst(self, root: TreeNode) -> TreeNode:
88+
tot = 0
89+
def dfs(root):
90+
nonlocal tot
91+
if not root: return
92+
dfs(root.right)
93+
tot += root.val
94+
root.val = tot
95+
dfs(root.left)
96+
dfs(root)
97+
return root
98+
```
99+
TypeScript 代码:
100+
```TypeScript
101+
function bstToGst(root: TreeNode | null): TreeNode | null {
102+
let tot = 0;
103+
const dfs = function(root: TreeNode | null): void {
104+
if (!root) return ;
105+
dfs(root.right);
106+
tot += root.val;
107+
root.val = tot;
108+
dfs(root.left);
109+
}
110+
dfs(root);
111+
return root;
112+
};
113+
```
114+
* 时间复杂度:$O(n)$
115+
* 空间复杂度:$O(n)$
116+
117+
---
118+
119+
### 最后
120+
121+
这是我们「刷穿 LeetCode」系列文章的第 `No.1038` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
122+
123+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
124+
125+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
126+
127+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
128+

0 commit comments

Comments
 (0)