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 ad26a58

Browse files
feat: add solutions to lcof2 problem: No.052
1 parent aade7c0 commit ad26a58

File tree

5 files changed

+267
-1
lines changed

5 files changed

+267
-1
lines changed

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

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,160 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
由于二叉搜索树的性质,可以利用中序遍历得到递增序列
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

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

5355
```python
54-
56+
# Definition for a binary tree node.
57+
# class TreeNode:
58+
# def __init__(self, val=0, left=None, right=None):
59+
# self.val = val
60+
# self.left = left
61+
# self.right = right
62+
class Solution:
63+
def increasingBST(self, root: TreeNode) -> TreeNode:
64+
head, tail = None, None
65+
stack = []
66+
cur = root
67+
while stack or cur:
68+
while cur:
69+
stack.append(cur)
70+
cur = cur.left
71+
cur = stack.pop()
72+
if not head:
73+
head = cur
74+
else:
75+
tail.right = cur
76+
tail = cur
77+
cur.left = None
78+
cur = cur.right
79+
return head
5580
```
5681

5782
### **Java**
5883

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

6186
```java
87+
/**
88+
* Definition for a binary tree node.
89+
* public class TreeNode {
90+
* int val;
91+
* TreeNode left;
92+
* TreeNode right;
93+
* TreeNode() {}
94+
* TreeNode(int val) { this.val = val; }
95+
* TreeNode(int val, TreeNode left, TreeNode right) {
96+
* this.val = val;
97+
* this.left = left;
98+
* this.right = right;
99+
* }
100+
* }
101+
*/
102+
class Solution {
103+
public TreeNode increasingBST(TreeNode root) {
104+
TreeNode head = null, tail = null;
105+
Deque<TreeNode> stack = new ArrayDeque<>();
106+
TreeNode cur = root;
107+
while (!stack.isEmpty() || cur != null) {
108+
while (cur != null) {
109+
stack.push(cur);
110+
cur = cur.left;
111+
}
112+
cur = stack.pop();
113+
if (head == null) {
114+
head = cur;
115+
} else {
116+
tail.right = cur;
117+
}
118+
tail = cur;
119+
cur.left = null;
120+
cur = cur.right;
121+
}
122+
return head;
123+
}
124+
}
125+
```
126+
127+
### **Go**
128+
129+
```go
130+
/**
131+
* Definition for a binary tree node.
132+
* type TreeNode struct {
133+
* Val int
134+
* Left *TreeNode
135+
* Right *TreeNode
136+
* }
137+
*/
138+
func increasingBST(root *TreeNode) *TreeNode {
139+
var head, tail *TreeNode
140+
stack := make([]*TreeNode, 0)
141+
cur := root
142+
for len(stack) > 0 || cur != nil {
143+
for cur != nil {
144+
stack = append(stack, cur)
145+
cur = cur.Left
146+
}
147+
cur = stack[len(stack)-1]
148+
stack = stack[:len(stack)-1]
149+
if head == nil {
150+
head = cur
151+
} else {
152+
tail.Right = cur
153+
}
154+
tail = cur
155+
cur.Left = nil
156+
cur = cur.Right
157+
}
158+
return head
159+
}
160+
```
62161

162+
### **C++**
163+
164+
```cpp
165+
/**
166+
* Definition for a binary tree node.
167+
* struct TreeNode {
168+
* int val;
169+
* TreeNode *left;
170+
* TreeNode *right;
171+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
172+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
173+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
174+
* };
175+
*/
176+
class Solution {
177+
public:
178+
TreeNode* increasingBST(TreeNode* root) {
179+
TreeNode *head = nullptr, *tail = nullptr;
180+
stack<TreeNode*> stk;
181+
TreeNode* cur = root;
182+
while (!stk.empty() || cur != nullptr) {
183+
while (cur != nullptr) {
184+
stk.push(cur);
185+
cur = cur->left;
186+
}
187+
cur = stk.top();
188+
stk.pop();
189+
if (head == nullptr) {
190+
head = cur;
191+
} else {
192+
tail->right = cur;
193+
}
194+
tail = cur;
195+
cur->left = nullptr;
196+
cur = cur->right;
197+
}
198+
return head;
199+
}
200+
};
63201
```
64202
65203
### **...**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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* increasingBST(TreeNode* root) {
15+
TreeNode *head = nullptr, *tail = nullptr;
16+
stack<TreeNode*> stk;
17+
TreeNode* cur = root;
18+
while (!stk.empty() || cur != nullptr) {
19+
while (cur != nullptr) {
20+
stk.push(cur);
21+
cur = cur->left;
22+
}
23+
cur = stk.top();
24+
stk.pop();
25+
if (head == nullptr) {
26+
head = cur;
27+
} else {
28+
tail->right = cur;
29+
}
30+
tail = cur;
31+
cur->left = nullptr;
32+
cur = cur->right;
33+
}
34+
return head;
35+
}
36+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 increasingBST(root *TreeNode) *TreeNode {
10+
var head, tail *TreeNode
11+
stack := make([]*TreeNode, 0)
12+
cur := root
13+
for len(stack) > 0 || cur != nil {
14+
for cur != nil {
15+
stack = append(stack, cur)
16+
cur = cur.Left
17+
}
18+
cur = stack[len(stack)-1]
19+
stack = stack[:len(stack)-1]
20+
if head == nil {
21+
head = cur
22+
} else {
23+
tail.Right = cur
24+
}
25+
tail = cur
26+
cur.Left = nil
27+
cur = cur.Right
28+
}
29+
return head
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode increasingBST(TreeNode root) {
18+
TreeNode head = null, tail = null;
19+
Deque<TreeNode> stack = new ArrayDeque<>();
20+
TreeNode cur = root;
21+
while (!stack.isEmpty() || cur != null) {
22+
while (cur != null) {
23+
stack.push(cur);
24+
cur = cur.left;
25+
}
26+
cur = stack.pop();
27+
if (head == null) {
28+
head = cur;
29+
} else {
30+
tail.right = cur;
31+
}
32+
tail = cur;
33+
cur.left = null;
34+
cur = cur.right;
35+
}
36+
return head;
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
def increasingBST(self, root: TreeNode) -> TreeNode:
9+
head, tail = None, None
10+
stack = []
11+
cur = root
12+
while stack or cur:
13+
while cur:
14+
stack.append(cur)
15+
cur = cur.left
16+
cur = stack.pop()
17+
if not head:
18+
head = cur
19+
else:
20+
tail.right = cur
21+
tail = cur
22+
cur.left = None
23+
cur = cur.right
24+
return head

0 commit comments

Comments
(0)

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