Skip to content

Commit 158fc4c

Browse files
authored
feat: add solutions to lcof2 problem: No.052 (doocs#1466)
No.052.展开二叉搜索树
1 parent 54a0aff commit 158fc4c

File tree

1 file changed

+127
-0
lines changed
  • lcof2/剑指 Offer II 052. 展平二叉搜索树

1 file changed

+127
-0
lines changed

lcof2/剑指 Offer II 052. 展平二叉搜索树/README.md

+127
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ class Solution:
7878
return head
7979
```
8080

81+
```python
82+
# Definition for a binary tree node.
83+
# class TreeNode:
84+
# def __init__(self, val=0, left=None, right=None):
85+
# self.val = val
86+
# self.left = left
87+
# self.right = right
88+
class Solution:
89+
def increasingBST(self, root: TreeNode) -> TreeNode:
90+
def dfs(root: TreeNode):
91+
if root is None:
92+
return
93+
94+
dfs(root.left)
95+
96+
nonlocal cur
97+
cur.right = root
98+
root.left = None
99+
cur = cur.right
100+
101+
dfs(root.right)
102+
103+
cur = dummy = TreeNode()
104+
dfs(root)
105+
return dummy.right
106+
```
107+
81108
### **Java**
82109

83110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -123,6 +150,45 @@ class Solution {
123150
}
124151
```
125152

153+
```java
154+
/**
155+
* Definition for a binary tree node.
156+
* public class TreeNode {
157+
* int val;
158+
* TreeNode left;
159+
* TreeNode right;
160+
* TreeNode() {}
161+
* TreeNode(int val) { this.val = val; }
162+
* TreeNode(int val, TreeNode left, TreeNode right) {
163+
* this.val = val;
164+
* this.left = left;
165+
* this.right = right;
166+
* }
167+
* }
168+
*/
169+
class Solution {
170+
private TreeNode cur;
171+
172+
public TreeNode increasingBST(TreeNode root) {
173+
TreeNode dummy = new TreeNode();
174+
cur = dummy;
175+
dfs(root);
176+
return dummy.right;
177+
}
178+
179+
private void dfs(TreeNode root) {
180+
if (root == null) {
181+
return;
182+
}
183+
dfs(root.left);
184+
cur.right = root;
185+
root.left = null;
186+
cur = cur.right;
187+
dfs(root.right);
188+
}
189+
}
190+
```
191+
126192
### **Go**
127193

128194
```go
@@ -158,6 +224,34 @@ func increasingBST(root *TreeNode) *TreeNode {
158224
}
159225
```
160226

227+
```go
228+
/**
229+
* Definition for a binary tree node.
230+
* type TreeNode struct {
231+
* Val int
232+
* Left *TreeNode
233+
* Right *TreeNode
234+
* }
235+
*/
236+
func increasingBST(root *TreeNode) *TreeNode {
237+
dummy := &TreeNode{}
238+
cur := dummy
239+
var dfs func(*TreeNode)
240+
dfs = func(root *TreeNode) {
241+
if root == nil {
242+
return
243+
}
244+
dfs(root.Left)
245+
root.Left = nil
246+
cur.Right = root
247+
cur = root
248+
dfs(root.Right)
249+
}
250+
dfs(root)
251+
return dummy.Right
252+
}
253+
```
254+
161255
### **C++**
162256

163257
```cpp
@@ -199,6 +293,39 @@ public:
199293
};
200294
```
201295
296+
```cpp
297+
/**
298+
* Definition for a binary tree node.
299+
* struct TreeNode {
300+
* int val;
301+
* TreeNode *left;
302+
* TreeNode *right;
303+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
304+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
305+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
306+
* };
307+
*/
308+
class Solution {
309+
public:
310+
TreeNode* increasingBST(TreeNode* root) {
311+
TreeNode* dummy = new TreeNode();
312+
TreeNode* cur = dummy;
313+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
314+
if (!root) {
315+
return;
316+
}
317+
dfs(root->left);
318+
cur->right = root;
319+
root->left = nullptr;
320+
cur = cur->right;
321+
dfs(root->right);
322+
};
323+
dfs(root);
324+
return dummy->right;
325+
}
326+
};
327+
```
328+
202329
### **TypeScript**
203330

204331
```ts

0 commit comments

Comments
 (0)