Skip to content

Commit 73c0a50

Browse files
committed
feat: add solutions to lcof problem: No.36
1 parent 11ef679 commit 73c0a50

File tree

7 files changed

+310
-147
lines changed

7 files changed

+310
-147
lines changed

lcof/面试题36. 二叉搜索树与双向链表/README.md

+167-81
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
- 排序链表:二叉搜索树中序遍历得到有序序列
42-
- 循环链表:头节点指向链表尾节点,尾节点指向链表头节点
43-
- 双向链表:`pre.right = cur``cur.left = pre``pre = cur`
41+
**方法一:中序遍历**
42+
43+
二叉搜索树的中序遍历是有序序列,因此可以通过中序遍历得到有序序列,过程中构建双向链表。
44+
45+
遍历结束,将头节点和尾节点相连,返回头节点。
46+
47+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉搜索树的节点个数。
4448

4549
<!-- tabs:start -->
4650

@@ -60,26 +64,27 @@ class Node:
6064

6165

6266
class Solution:
63-
def treeToDoublyList(self, root: 'Node') -> 'Node':
64-
def dfs(cur):
65-
if cur is None:
67+
def treeToDoublyList(self, root: "Node") -> "Node":
68+
def dfs(root):
69+
if root is None:
6670
return
67-
dfs(cur.left)
68-
if self.pre is None:
69-
self.head = cur
71+
dfs(root.left)
72+
nonlocal head, pre
73+
if pre:
74+
pre.right = root
7075
else:
71-
self.pre.right = cur
72-
cur.left = self.pre
73-
self.pre = cur
74-
dfs(cur.right)
76+
head = root
77+
root.left = pre
78+
pre = root
79+
dfs(root.right)
7580

7681
if root is None:
7782
return None
78-
self.head = self.pre = None
83+
head = pre = None
7984
dfs(root)
80-
self.head.left = self.pre
81-
self.pre.right = self.head
82-
return self.head
85+
head.left = pre
86+
pre.right = head
87+
return head
8388
```
8489

8590
### **Java**
@@ -108,27 +113,129 @@ class Node {
108113
};
109114
*/
110115
class Solution {
111-
Node head;
112-
Node pre;
116+
private Node head;
117+
private Node pre;
118+
113119
public Node treeToDoublyList(Node root) {
114-
if (root == null) return null;
120+
if (root == null) {
121+
return null;
122+
}
115123
dfs(root);
116124
head.left = pre;
117125
pre.right = head;
118126
return head;
119127
}
120128

121-
private void dfs(Node cur) {
122-
if (cur == null) return;
123-
dfs(cur.left);
124-
if (pre == null)
125-
head = cur;
126-
else
127-
pre.right = cur;
128-
cur.left = pre;
129-
pre = cur;
130-
dfs(cur.right);
129+
private void dfs(Node root) {
130+
if (root == null) {
131+
return;
132+
}
133+
dfs(root.left);
134+
if (pre != null) {
135+
pre.right = root;
136+
} else {
137+
head = root;
138+
}
139+
root.left = pre;
140+
pre = root;
141+
dfs(root.right);
142+
}
143+
}
144+
```
145+
146+
### **C++**
147+
148+
```cpp
149+
/*
150+
// Definition for a Node.
151+
class Node {
152+
public:
153+
int val;
154+
Node* left;
155+
Node* right;
156+
157+
Node() {}
158+
159+
Node(int _val) {
160+
val = _val;
161+
left = NULL;
162+
right = NULL;
163+
}
164+
165+
Node(int _val, Node* _left, Node* _right) {
166+
val = _val;
167+
left = _left;
168+
right = _right;
169+
}
170+
};
171+
*/
172+
class Solution {
173+
public:
174+
Node* treeToDoublyList(Node* root) {
175+
if (!root) {
176+
return nullptr;
177+
}
178+
Node* pre = nullptr;
179+
Node* head = nullptr;
180+
function<void(Node*)> dfs = [&](Node* root) {
181+
if (!root) {
182+
return;
183+
}
184+
dfs(root->left);
185+
if (pre) {
186+
pre->right = root;
187+
} else {
188+
head = root;
189+
}
190+
root->left = pre;
191+
pre = root;
192+
dfs(root->right);
193+
};
194+
195+
dfs(root);
196+
head->left = pre;
197+
pre->right = head;
198+
return head;
131199
}
200+
};
201+
```
202+
203+
### **Go**
204+
205+
```go
206+
/**
207+
* Definition for a Node.
208+
* type Node struct {
209+
* Val int
210+
* Left *Node
211+
* Right *Node
212+
* }
213+
*/
214+
215+
func treeToDoublyList(root *Node) *Node {
216+
if root == nil {
217+
return nil
218+
}
219+
var head, pre *Node
220+
var dfs func(*Node)
221+
dfs = func(root *Node) {
222+
if root == nil {
223+
return
224+
}
225+
dfs(root.Left)
226+
if pre != nil {
227+
pre.Right = root
228+
} else {
229+
head = root
230+
}
231+
root.Left = pre
232+
pre = root
233+
dfs(root.Right)
234+
}
235+
dfs(root)
236+
head.Left = pre
237+
pre.Right = head
238+
return head
132239
}
133240
```
134241

@@ -148,55 +255,32 @@ class Solution {
148255
* @return {Node}
149256
*/
150257
var treeToDoublyList = function (root) {
151-
function dfs(cur) {
152-
if (!cur) return;
153-
dfs(cur.left);
154-
if (!pre) head = cur;
155-
else pre.right = cur;
156-
cur.left = pre;
157-
pre = cur;
158-
dfs(cur.right);
258+
if (!root) {
259+
return null;
159260
}
160-
if (!root) return null;
161-
let head, pre;
261+
let head = null;
262+
let pre = null;
263+
const dfs = root => {
264+
if (!root) {
265+
return;
266+
}
267+
dfs(root.left);
268+
if (pre) {
269+
pre.right = root;
270+
} else {
271+
head = root;
272+
}
273+
root.left = pre;
274+
pre = root;
275+
dfs(root.right);
276+
};
162277
dfs(root);
163278
head.left = pre;
164279
pre.right = head;
165280
return head;
166281
};
167282
```
168283

169-
### **C++**
170-
171-
```cpp
172-
class Solution {
173-
public:
174-
Node* treeToDoublyList(Node* root) {
175-
if (root == NULL) return NULL;
176-
inorder(root);
177-
head->left = pre;
178-
pre->right = head;
179-
return head;
180-
}
181-
182-
private:
183-
Node *pre, *head;
184-
185-
void inorder(Node* cur) {
186-
if (cur) {
187-
inorder(cur->left);
188-
if (pre)
189-
pre->right = cur;
190-
else
191-
head = cur;
192-
cur->left = pre;
193-
pre = cur;
194-
inorder(cur->right);
195-
}
196-
}
197-
};
198-
```
199-
200284
### **C#**
201285

202286
```cs
@@ -224,7 +308,9 @@ public class Node {
224308
*/
225309

226310
public class Solution {
227-
Node head, pre;
311+
private Node head;
312+
private Node pre;
313+
228314
public Node TreeToDoublyList(Node root) {
229315
if (root == null) {
230316
return null;
@@ -235,19 +321,19 @@ public class Solution {
235321
return head;
236322
}
237323

238-
public void dfs(Node cur) {
239-
if (cur == null) {
324+
private void dfs(Node root) {
325+
if (root == null) {
240326
return;
241327
}
242-
dfs(cur.left);
243-
if (pre == null) {
244-
head = cur;
328+
dfs(root.left);
329+
if (pre != null) {
330+
pre.right = root;
245331
} else {
246-
pre.right = cur;
332+
head = root;
247333
}
248-
cur.left = pre;
249-
pre = cur;
250-
dfs(cur.right);
334+
root.left = pre;
335+
pre = root;
336+
dfs(root.right);
251337
}
252338
}
253339
```
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
9+
Node() {}
10+
11+
Node(int _val) {
12+
val = _val;
13+
left = NULL;
14+
right = NULL;
15+
}
16+
17+
Node(int _val, Node* _left, Node* _right) {
18+
val = _val;
19+
left = _left;
20+
right = _right;
21+
}
22+
};
23+
*/
124
class Solution {
225
public:
326
Node* treeToDoublyList(Node* root) {
4-
if (root == NULL) return NULL;
5-
inorder(root);
27+
if (!root) {
28+
return nullptr;
29+
}
30+
Node* pre = nullptr;
31+
Node* head = nullptr;
32+
function<void(Node*)> dfs = [&](Node* root) {
33+
if (!root) {
34+
return;
35+
}
36+
dfs(root->left);
37+
if (pre) {
38+
pre->right = root;
39+
} else {
40+
head = root;
41+
}
42+
root->left = pre;
43+
pre = root;
44+
dfs(root->right);
45+
};
46+
47+
dfs(root);
648
head->left = pre;
749
pre->right = head;
850
return head;
951
}
10-
11-
private:
12-
Node *pre, *head;
13-
14-
void inorder(Node* cur) {
15-
if (cur) {
16-
inorder(cur->left);
17-
if (pre)
18-
pre->right = cur;
19-
else
20-
head = cur;
21-
cur->left = pre;
22-
pre = cur;
23-
inorder(cur->right);
24-
}
25-
}
26-
};
52+
};

0 commit comments

Comments
 (0)