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 f85d9a0

Browse files
committed
feat: add solutions to lc problems: No.0538,1038
1 parent b74ee20 commit f85d9a0

File tree

11 files changed

+439
-89
lines changed

11 files changed

+439
-89
lines changed

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<li>给定的树为二叉搜索树。</li>
5858
</ul>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
@@ -69,7 +68,21 @@
6968
<!-- 这里可写当前语言的特殊实现逻辑 -->
7069

7170
```python
72-
71+
# Definition for a binary tree node.
72+
# class TreeNode:
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
77+
class Solution:
78+
add = 0
79+
def convertBST(self, root: TreeNode) -> TreeNode:
80+
if root:
81+
self.convertBST(root.right)
82+
root.val += self.add
83+
self.add = root.val
84+
self.convertBST(root.left)
85+
return root
7386
```
7487

7588
### **Java**
@@ -91,6 +104,35 @@ class Solution {
91104
}
92105
```
93106

107+
### **C++**
108+
109+
```cpp
110+
/**
111+
* Definition for a binary tree node.
112+
* struct TreeNode {
113+
* int val;
114+
* TreeNode *left;
115+
* TreeNode *right;
116+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
117+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
118+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
119+
* };
120+
*/
121+
class Solution {
122+
public:
123+
int add = 0;
124+
TreeNode* convertBST(TreeNode* root) {
125+
if (root) {
126+
convertBST(root->right);
127+
root->val += add;
128+
add = root->val;
129+
convertBST(root->left);
130+
}
131+
return root;
132+
}
133+
};
134+
```
135+
94136
### **...**
95137
96138
```

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

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66

77
<p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.</p>
88

9-
10-
119
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>
1210

13-
14-
1511
<ul>
1612
<li>The left subtree of a node contains only nodes with keys&nbsp;<strong>less than</strong>&nbsp;the node&#39;s key.</li>
1713
<li>The right subtree of a node contains only nodes with keys&nbsp;<strong>greater than</strong>&nbsp;the node&#39;s key.</li>
1814
<li>Both the left and right subtrees must also be binary search trees.</li>
1915
</ul>
2016

21-
22-
2317
<p><strong>Note:</strong> This question is the same as&nbsp;1038:&nbsp;<a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>
2418

25-
26-
2719
<p>&nbsp;</p>
2820

2921
<p><strong>Example 1:</strong></p>
@@ -38,12 +30,8 @@
3830

3931
</pre>
4032

41-
42-
4333
<p><strong>Example 2:</strong></p>
4434

45-
46-
4735
<pre>
4836

4937
<strong>Input:</strong> root = [0,null,1]
@@ -52,12 +40,8 @@
5240

5341
</pre>
5442

55-
56-
5743
<p><strong>Example 3:</strong></p>
5844

59-
60-
6145
<pre>
6246

6347
<strong>Input:</strong> root = [1,0,2]
@@ -66,12 +50,8 @@
6650

6751
</pre>
6852

69-
70-
7153
<p><strong>Example 4:</strong></p>
7254

73-
74-
7555
<pre>
7656

7757
<strong>Input:</strong> root = [3,2,4,1]
@@ -80,14 +60,10 @@
8060

8161
</pre>
8262

83-
84-
8563
<p>&nbsp;</p>
8664

8765
<p><strong>Constraints:</strong></p>
8866

89-
90-
9167
<ul>
9268
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
9369
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
@@ -102,7 +78,21 @@
10278
### **Python3**
10379

10480
```python
105-
81+
# Definition for a binary tree node.
82+
# class TreeNode:
83+
# def __init__(self, val=0, left=None, right=None):
84+
# self.val = val
85+
# self.left = left
86+
# self.right = right
87+
class Solution:
88+
add = 0
89+
def convertBST(self, root: TreeNode) -> TreeNode:
90+
if root:
91+
self.convertBST(root.right)
92+
root.val += self.add
93+
self.add = root.val
94+
self.convertBST(root.left)
95+
return root
10696
```
10797

10898
### **Java**
@@ -122,6 +112,62 @@ class Solution {
122112
}
123113
```
124114

115+
### **C++**
116+
117+
```cpp
118+
/**
119+
* Definition for a binary tree node.
120+
* struct TreeNode {
121+
* int val;
122+
* TreeNode *left;
123+
* TreeNode *right;
124+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
127+
* };
128+
*/
129+
class Solution {
130+
public:
131+
int add = 0;
132+
TreeNode* convertBST(TreeNode* root) {
133+
if (root) {
134+
convertBST(root->right);
135+
root->val += add;
136+
add = root->val;
137+
convertBST(root->left);
138+
}
139+
return root;
140+
}
141+
};
142+
```
143+
144+
### **Go**
145+
146+
```go
147+
/**
148+
* Definition for a binary tree node.
149+
* type TreeNode struct {
150+
* Val int
151+
* Left *TreeNode
152+
* Right *TreeNode
153+
* }
154+
*/
155+
func convertBST(root *TreeNode) *TreeNode {
156+
add := 0
157+
var dfs func(*TreeNode)
158+
dfs = func(node *TreeNode) {
159+
if node != nil {
160+
dfs(node.Right)
161+
node.Val += add
162+
add = node.Val
163+
dfs(node.Left)
164+
}
165+
}
166+
dfs(root)
167+
return root
168+
}
169+
```
170+
125171
### **...**
126172

127173
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
int add = 0;
15+
TreeNode* convertBST(TreeNode* root) {
16+
if (root) {
17+
convertBST(root->right);
18+
root->val += add;
19+
add = root->val;
20+
convertBST(root->left);
21+
}
22+
return root;
23+
}
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
add := 0
11+
var dfs func(*TreeNode)
12+
dfs = func(node *TreeNode) {
13+
if node != nil {
14+
dfs(node.Right)
15+
node.Val += add
16+
add = node.Val
17+
dfs(node.Left)
18+
}
19+
}
20+
dfs(root)
21+
return root
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
add = 0
9+
10+
def convertBST(self, root: TreeNode) -> TreeNode:
11+
if root:
12+
self.convertBST(root.right)
13+
root.val += self.add
14+
self.add = root.val
15+
self.convertBST(root.left)
16+
return root

0 commit comments

Comments
(0)

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