Skip to content

Commit 6102880

Browse files
committed
feat: add solutions to lcof2 problem: No.054
1 parent c93f25e commit 6102880

File tree

6 files changed

+408
-1
lines changed

6 files changed

+408
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
## 站点
2222

2323
- Netlify: https://lc.netlify.app
24+
- ~~Gitee Pages: https://doocs.gitee.io/leetcode~~
2425
- GitHub Pages: https://doocs.github.io/leetcode
2526

27+
注:Gitee Pages 站点被 Gitee 官方误判为“包含违禁违规内容”,惨遭下线。
28+
2629
## LeetCode 全解
2730

2831
- [LeetCode](./solution/README.md)

lcof2/剑指 Offer II 054. 所有大于等于节点的值之和/README.md

Lines changed: 252 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,278 @@
6868
<li>本题与主站 1038&nbsp;题相同:<a href="https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/">https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/</a></li>
6969
</ul>
7070

71-
7271
## 解法
7372

7473
<!-- 这里可写通用的实现逻辑 -->
7574

75+
二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。
76+
77+
关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。
78+
79+
Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
80+
81+
定义 s 表示二叉搜索树节点值累加之和。遍历二叉树节点,
82+
83+
1. 若当前节点 root 的右子树为空,**将当前节点值添加至 s** 中,更新当前节点值为 s,并将当前节点更新为 `root.left`
84+
2. 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点):
85+
- 若后继节点 next 的左子树为空,将后继节点的左子树指向当前节点 root,并将当前节点更新为 `root.right`
86+
- 若后继节点 next 的左子树不为空,**将当前节点值添加 s** 中,更新当前节点值为 s,然后将后继节点左子树指向空(即解除 next 与 root 的指向关系),并将当前节点更新为 `root.left`
87+
3. 循环以上步骤,直至二叉树节点为空,遍历结束。
88+
4. 最后返回二叉搜索树根节点即可。
89+
90+
> Morris 反序中序遍历跟 Morris 中序遍历思路一致,只是将中序遍历的“左根右”变为“右根左”。
91+
7692
<!-- tabs:start -->
7793

7894
### **Python3**
7995

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

98+
递归遍历:
99+
82100
```python
101+
# Definition for a binary tree node.
102+
# class TreeNode:
103+
# def __init__(self, val=0, left=None, right=None):
104+
# self.val = val
105+
# self.left = left
106+
# self.right = right
107+
class Solution:
108+
add = 0
109+
def convertBST(self, root: TreeNode) -> TreeNode:
110+
if root:
111+
self.convertBST(root.right)
112+
root.val += self.add
113+
self.add = root.val
114+
self.convertBST(root.left)
115+
return root
116+
```
83117

118+
Morris 遍历:
119+
120+
```python
121+
# Definition for a binary tree node.
122+
# class TreeNode:
123+
# def __init__(self, val=0, left=None, right=None):
124+
# self.val = val
125+
# self.left = left
126+
# self.right = right
127+
class Solution:
128+
def convertBST(self, root: TreeNode) -> TreeNode:
129+
s = 0
130+
node = root
131+
while root:
132+
if root.right is None:
133+
s += root.val
134+
root.val = s
135+
root = root.left
136+
else:
137+
next = root.right
138+
while next.left and next.left != root:
139+
next = next.left
140+
if next.left is None:
141+
next.left = root
142+
root = root.right
143+
else:
144+
s += root.val
145+
root.val = s
146+
next.left = None
147+
root = root.left
148+
return node
84149
```
85150

86151
### **Java**
87152

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

155+
递归遍历:
156+
157+
```java
158+
class Solution {
159+
int add = 0;
160+
public TreeNode convertBST(TreeNode root) {
161+
if (root != null) {
162+
convertBST(root.right);
163+
root.val += add;
164+
add = root.val;
165+
convertBST(root.left);
166+
}
167+
return root;
168+
}
169+
}
170+
```
171+
172+
Morris 遍历:
173+
90174
```java
175+
/**
176+
* Definition for a binary tree node.
177+
* public class TreeNode {
178+
* int val;
179+
* TreeNode left;
180+
* TreeNode right;
181+
* TreeNode() {}
182+
* TreeNode(int val) { this.val = val; }
183+
* TreeNode(int val, TreeNode left, TreeNode right) {
184+
* this.val = val;
185+
* this.left = left;
186+
* this.right = right;
187+
* }
188+
* }
189+
*/
190+
class Solution {
191+
public TreeNode convertBST(TreeNode root) {
192+
int s = 0;
193+
TreeNode node = root;
194+
while (root != null) {
195+
if (root.right == null) {
196+
s += root.val;
197+
root.val = s;
198+
root = root.left;
199+
} else {
200+
TreeNode next = root.right;
201+
while (next.left != null && next.left != root) {
202+
next = next.left;
203+
}
204+
if (next.left == null) {
205+
next.left = root;
206+
root = root.right;
207+
} else {
208+
s += root.val;
209+
root.val = s;
210+
next.left = null;
211+
root = root.left;
212+
}
213+
}
214+
}
215+
return node;
216+
}
217+
}
218+
```
219+
220+
### **C++**
221+
222+
递归遍历:
223+
224+
```cpp
225+
/**
226+
* Definition for a binary tree node.
227+
* struct TreeNode {
228+
* int val;
229+
* TreeNode *left;
230+
* TreeNode *right;
231+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
232+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
233+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
234+
* };
235+
*/
236+
class Solution {
237+
public:
238+
int add = 0;
239+
TreeNode* convertBST(TreeNode* root) {
240+
if (root) {
241+
convertBST(root->right);
242+
root->val += add;
243+
add = root->val;
244+
convertBST(root->left);
245+
}
246+
return root;
247+
}
248+
};
249+
```
250+
251+
Morris 遍历:
252+
253+
```cpp
254+
/**
255+
* Definition for a binary tree node.
256+
* struct TreeNode {
257+
* int val;
258+
* TreeNode *left;
259+
* TreeNode *right;
260+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
261+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
262+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
263+
* };
264+
*/
265+
class Solution {
266+
public:
267+
TreeNode *convertBST(TreeNode *root) {
268+
int s = 0;
269+
TreeNode *node = root;
270+
while (root)
271+
{
272+
if (root->right == nullptr)
273+
{
274+
s += root->val;
275+
root->val = s;
276+
root = root->left;
277+
}
278+
else
279+
{
280+
TreeNode *next = root->right;
281+
while (next->left && next->left != root)
282+
{
283+
next = next->left;
284+
}
285+
if (next->left == nullptr)
286+
{
287+
next->left = root;
288+
root = root->right;
289+
}
290+
else
291+
{
292+
s += root->val;
293+
root->val = s;
294+
next->left = nullptr;
295+
root = root->left;
296+
}
297+
}
298+
}
299+
return node;
300+
}
301+
};
302+
```
91303

304+
### **Go**
305+
306+
Morris 遍历:
307+
308+
```go
309+
/**
310+
* Definition for a binary tree node.
311+
* type TreeNode struct {
312+
* Val int
313+
* Left *TreeNode
314+
* Right *TreeNode
315+
* }
316+
*/
317+
func convertBST(root *TreeNode) *TreeNode {
318+
s := 0
319+
node := root
320+
for root != nil {
321+
if root.Right == nil {
322+
s += root.Val
323+
root.Val = s
324+
root = root.Left
325+
} else {
326+
next := root.Right
327+
for next.Left != nil && next.Left != root {
328+
next = next.Left
329+
}
330+
if next.Left == nil {
331+
next.Left = root
332+
root = root.Right
333+
} else {
334+
s += root.Val
335+
root.Val = s
336+
next.Left = nil
337+
root = root.Left
338+
}
339+
}
340+
}
341+
return node
342+
}
92343
```
93344

94345
### **...**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode *convertBST(TreeNode *root) {
15+
int s = 0;
16+
TreeNode *node = root;
17+
while (root)
18+
{
19+
if (root->right == nullptr)
20+
{
21+
s += root->val;
22+
root->val = s;
23+
root = root->left;
24+
}
25+
else
26+
{
27+
TreeNode *next = root->right;
28+
while (next->left && next->left != root)
29+
{
30+
next = next->left;
31+
}
32+
if (next->left == nullptr)
33+
{
34+
next->left = root;
35+
root = root->right;
36+
}
37+
else
38+
{
39+
s += root->val;
40+
root->val = s;
41+
next->left = nullptr;
42+
root = root->left;
43+
}
44+
}
45+
}
46+
return node;
47+
}
48+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func convertBST(root *TreeNode) *TreeNode {
10+
s := 0
11+
node := root
12+
for root != nil {
13+
if root.Right == nil {
14+
s += root.Val
15+
root.Val = s
16+
root = root.Left
17+
} else {
18+
next := root.Right
19+
for next.Left != nil && next.Left != root {
20+
next = next.Left
21+
}
22+
if next.Left == nil {
23+
next.Left = root
24+
root = root.Right
25+
} else {
26+
s += root.Val
27+
root.Val = s
28+
next.Left = nil
29+
root = root.Left
30+
}
31+
}
32+
}
33+
return node
34+
}

0 commit comments

Comments
 (0)