Skip to content

Commit d946c12

Browse files
committed
feat: add solutions to lc problems: No.0538,1038
1 parent 2d9662a commit d946c12

File tree

12 files changed

+898
-66
lines changed

12 files changed

+898
-66
lines changed

solution/0500-0599/0538.Convert BST to Greater Tree/README.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。
65+
66+
关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。
67+
6468
<!-- tabs:start -->
6569

6670
### **Python3**
6771

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

74+
递归遍历:
75+
7076
```python
7177
# Definition for a binary tree node.
7278
# class TreeNode:
@@ -85,10 +91,45 @@ class Solution:
8591
return root
8692
```
8793

94+
Morris 遍历:
95+
96+
```python
97+
# Definition for a binary tree node.
98+
# class TreeNode:
99+
# def __init__(self, val=0, left=None, right=None):
100+
# self.val = val
101+
# self.left = left
102+
# self.right = right
103+
class Solution:
104+
def convertBST(self, root: TreeNode) -> TreeNode:
105+
s = 0
106+
node = root
107+
while root:
108+
if root.right is None:
109+
s += root.val
110+
root.val = s
111+
root = root.left
112+
else:
113+
next = root.right
114+
while next.left and next.left != root:
115+
next = next.left
116+
if next.left is None:
117+
next.left = root
118+
root = root.right
119+
else:
120+
s += root.val
121+
root.val = s
122+
next.left = None
123+
root = root.left
124+
return node
125+
```
126+
88127
### **Java**
89128

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

131+
递归遍历:
132+
92133
```java
93134
class Solution {
94135
int add = 0;
@@ -104,8 +145,58 @@ class Solution {
104145
}
105146
```
106147

148+
Morris 遍历:
149+
150+
```java
151+
/**
152+
* Definition for a binary tree node.
153+
* public class TreeNode {
154+
* int val;
155+
* TreeNode left;
156+
* TreeNode right;
157+
* TreeNode() {}
158+
* TreeNode(int val) { this.val = val; }
159+
* TreeNode(int val, TreeNode left, TreeNode right) {
160+
* this.val = val;
161+
* this.left = left;
162+
* this.right = right;
163+
* }
164+
* }
165+
*/
166+
class Solution {
167+
public TreeNode convertBST(TreeNode root) {
168+
int s = 0;
169+
TreeNode node = root;
170+
while (root != null) {
171+
if (root.right == null) {
172+
s += root.val;
173+
root.val = s;
174+
root = root.left;
175+
} else {
176+
TreeNode next = root.right;
177+
while (next.left != null && next.left != root) {
178+
next = next.left;
179+
}
180+
if (next.left == null) {
181+
next.left = root;
182+
root = root.right;
183+
} else {
184+
s += root.val;
185+
root.val = s;
186+
next.left = null;
187+
root = root.left;
188+
}
189+
}
190+
}
191+
return node;
192+
}
193+
}
194+
```
195+
107196
### **C++**
108197

198+
递归遍历:
199+
109200
```cpp
110201
/**
111202
* Definition for a binary tree node.
@@ -133,6 +224,100 @@ public:
133224
};
134225
```
135226
227+
Morris 遍历:
228+
229+
```cpp
230+
/**
231+
* Definition for a binary tree node.
232+
* struct TreeNode {
233+
* int val;
234+
* TreeNode *left;
235+
* TreeNode *right;
236+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
237+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
238+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
239+
* };
240+
*/
241+
class Solution {
242+
public:
243+
TreeNode *convertBST(TreeNode *root) {
244+
int s = 0;
245+
TreeNode *node = root;
246+
while (root)
247+
{
248+
if (root->right == nullptr)
249+
{
250+
s += root->val;
251+
root->val = s;
252+
root = root->left;
253+
}
254+
else
255+
{
256+
TreeNode *next = root->right;
257+
while (next->left && next->left != root)
258+
{
259+
next = next->left;
260+
}
261+
if (next->left == nullptr)
262+
{
263+
next->left = root;
264+
root = root->right;
265+
}
266+
else
267+
{
268+
s += root->val;
269+
root->val = s;
270+
next->left = nullptr;
271+
root = root->left;
272+
}
273+
}
274+
}
275+
return node;
276+
}
277+
};
278+
```
279+
280+
### **Go**
281+
282+
Morris 遍历:
283+
284+
```go
285+
/**
286+
* Definition for a binary tree node.
287+
* type TreeNode struct {
288+
* Val int
289+
* Left *TreeNode
290+
* Right *TreeNode
291+
* }
292+
*/
293+
func convertBST(root *TreeNode) *TreeNode {
294+
s := 0
295+
node := root
296+
for root != nil {
297+
if root.Right == nil {
298+
s += root.Val
299+
root.Val = s
300+
root = root.Left
301+
} else {
302+
next := root.Right
303+
for next.Left != nil && next.Left != root {
304+
next = next.Left
305+
}
306+
if next.Left == nil {
307+
next.Left = root
308+
root = root.Right
309+
} else {
310+
s += root.Val
311+
root.Val = s
312+
next.Left = nil
313+
root = root.Left
314+
}
315+
}
316+
}
317+
return node
318+
}
319+
```
320+
136321
### **...**
137322

138323
```

0 commit comments

Comments
 (0)