Skip to content

Commit 6299e9a

Browse files
committed
feat: add solutions to lc problem: No.0116
No.0116.Populating Next Right Pointers in Each Node
1 parent a30818c commit 6299e9a

File tree

10 files changed

+479
-165
lines changed

10 files changed

+479
-165
lines changed

solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md

+219-59
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@ struct Node {
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66-
“BFS 层次遍历”实现。
66+
**方法一:BFS**
67+
68+
使用队列进行层序遍历,每次遍历一层时,将当前层的节点按顺序连接起来。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
71+
72+
**方法二:DFS**
73+
74+
使用递归进行前序遍历,每次遍历到一个节点时,将其左右子节点按顺序连接起来。
75+
76+
具体地,我们设计一个函数 $dfs(left, right)$,表示将 $left$ 节点的 $next$ 指针指向 $right$ 节点。在函数中,我们首先判断 $left$ 和 $right$ 是否为空,若都不为空,则将 $left.next$ 指向 $right$,然后递归地调用 $dfs(left.left, left.right)$, $dfs(left.right, right.left)$, $dfs(right.left, right.right)$。
77+
78+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
6779

6880
<!-- tabs:start -->
6981

@@ -84,21 +96,47 @@ class Node:
8496

8597

8698
class Solution:
87-
def connect(self, root: 'Node') -> 'Node':
88-
if root is None or (root.left is None and root.right is None):
99+
def connect(self, root: "Optional[Node]") -> "Optional[Node]":
100+
if root is None:
89101
return root
90102
q = deque([root])
91103
while q:
92-
size = len(q)
93-
cur = None
94-
for _ in range(size):
104+
p = None
105+
for _ in range(len(q)):
95106
node = q.popleft()
96-
if node.right:
97-
q.append(node.right)
107+
if p:
108+
p.next = node
109+
p = node
98110
if node.left:
99111
q.append(node.left)
100-
node.next = cur
101-
cur = node
112+
if node.right:
113+
q.append(node.right)
114+
return root
115+
```
116+
117+
```python
118+
"""
119+
# Definition for a Node.
120+
class Node:
121+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
122+
self.val = val
123+
self.left = left
124+
self.right = right
125+
self.next = next
126+
"""
127+
128+
class Solution:
129+
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
130+
def dfs(left, right):
131+
if left is None or right is None:
132+
return
133+
left.next = right
134+
dfs(left.left, left.right)
135+
dfs(left.right, right.left)
136+
dfs(right.left, right.right)
137+
138+
if root:
139+
dfs(root.left, root.right)
102140
return root
103141
```
104142

@@ -132,30 +170,76 @@ class Node {
132170

133171
class Solution {
134172
public Node connect(Node root) {
135-
if (root == null || (root.left == null && root.right == null)) {
173+
if (root == null) {
136174
return root;
137175
}
138176
Deque<Node> q = new ArrayDeque<>();
139177
q.offer(root);
140178
while (!q.isEmpty()) {
141-
Node cur = null;
142-
for (int i = 0, n = q.size(); i < n; ++i) {
143-
Node node = q.pollFirst();
144-
if (node.right != null) {
145-
q.offer(node.right);
179+
Node p = null;
180+
for (int n = q.size(); n > 0; --n) {
181+
Node node = q.poll();
182+
if (p != null) {
183+
p.next = node;
146184
}
185+
p = node;
147186
if (node.left != null) {
148187
q.offer(node.left);
149188
}
150-
node.next = cur;
151-
cur = node;
189+
if (node.right != null) {
190+
q.offer(node.right);
191+
}
152192
}
153193
}
154194
return root;
155195
}
156196
}
157197
```
158198

199+
```java
200+
/*
201+
// Definition for a Node.
202+
class Node {
203+
public int val;
204+
public Node left;
205+
public Node right;
206+
public Node next;
207+
208+
public Node() {}
209+
210+
public Node(int _val) {
211+
val = _val;
212+
}
213+
214+
public Node(int _val, Node _left, Node _right, Node _next) {
215+
val = _val;
216+
left = _left;
217+
right = _right;
218+
next = _next;
219+
}
220+
};
221+
*/
222+
223+
class Solution {
224+
public Node connect(Node root) {
225+
if (root != null) {
226+
dfs(root.left, root.right);
227+
}
228+
return root;
229+
}
230+
231+
private void dfs(Node left, Node right) {
232+
if (left == null || right == null) {
233+
return;
234+
}
235+
left.next = right;
236+
dfs(left.left, left.right);
237+
dfs(left.right, right.left);
238+
dfs(right.left, right.right);
239+
}
240+
}
241+
```
242+
159243
### **C++**
160244

161245
```cpp
@@ -180,31 +264,139 @@ public:
180264
class Solution {
181265
public:
182266
Node* connect(Node* root) {
183-
if (!root || (!root->left && !root->right)) {
267+
if (!root) {
184268
return root;
185269
}
186-
queue<Node*> q;
187-
q.push(root);
270+
queue<Node*> q{{root}};
188271
while (!q.empty()) {
189-
Node* cur = nullptr;
190-
for (int i = 0, n = q.size(); i < n; ++i) {
272+
Node* p = nullptr;
273+
for (int n = q.size(); n; --n) {
191274
Node* node = q.front();
192275
q.pop();
193-
if (node->right) {
194-
q.push(node->right);
276+
if (p) {
277+
p->next = node;
195278
}
279+
p = node;
196280
if (node->left) {
197281
q.push(node->left);
198282
}
199-
node->next = cur;
200-
cur = node;
283+
if (node->right) {
284+
q.push(node->right);
285+
}
201286
}
202287
}
203288
return root;
204289
}
205290
};
206291
```
207292
293+
```cpp
294+
/*
295+
// Definition for a Node.
296+
class Node {
297+
public:
298+
int val;
299+
Node* left;
300+
Node* right;
301+
Node* next;
302+
303+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
304+
305+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
306+
307+
Node(int _val, Node* _left, Node* _right, Node* _next)
308+
: val(_val), left(_left), right(_right), next(_next) {}
309+
};
310+
*/
311+
312+
class Solution {
313+
public:
314+
Node* connect(Node* root) {
315+
function<void(Node*, Node*)> dfs = [&](Node* left, Node* right) {
316+
if (!left || !right) {
317+
return;
318+
}
319+
left->next = right;
320+
dfs(left->left, left->right);
321+
dfs(left->right, right->left);
322+
dfs(right->left, right->right);
323+
};
324+
if (root) {
325+
dfs(root->left, root->right);
326+
}
327+
return root;
328+
}
329+
};
330+
```
331+
332+
### **Go**
333+
334+
```go
335+
/**
336+
* Definition for a Node.
337+
* type Node struct {
338+
* Val int
339+
* Left *Node
340+
* Right *Node
341+
* Next *Node
342+
* }
343+
*/
344+
345+
func connect(root *Node) *Node {
346+
if root == nil {
347+
return root
348+
}
349+
q := []*Node{root}
350+
for len(q) > 0 {
351+
var p *Node
352+
for n := len(q); n > 0; n-- {
353+
node := q[0]
354+
q = q[1:]
355+
if p != nil {
356+
p.Next = node
357+
}
358+
p = node
359+
if node.Left != nil {
360+
q = append(q, node.Left)
361+
}
362+
if node.Right != nil {
363+
q = append(q, node.Right)
364+
}
365+
}
366+
}
367+
return root
368+
}
369+
```
370+
371+
```go
372+
/**
373+
* Definition for a Node.
374+
* type Node struct {
375+
* Val int
376+
* Left *Node
377+
* Right *Node
378+
* Next *Node
379+
* }
380+
*/
381+
382+
func connect(root *Node) *Node {
383+
var dfs func(*Node, *Node)
384+
dfs = func(left, right *Node) {
385+
if left == nil || right == nil {
386+
return
387+
}
388+
left.Next = right
389+
dfs(left.Left, left.Right)
390+
dfs(left.Right, right.Left)
391+
dfs(right.Left, right.Right)
392+
}
393+
if root != nil {
394+
dfs(root.Left, root.Right)
395+
}
396+
return root
397+
}
398+
```
399+
208400
### **TypeScript**
209401

210402
```ts
@@ -276,38 +468,6 @@ function connect(root: Node | null): Node | null {
276468
}
277469
```
278470

279-
### **Go**
280-
281-
```go
282-
/**
283-
* Definition for a Node.
284-
* type Node struct {
285-
* Val int
286-
* Left *Node
287-
* Right *Node
288-
* Next *Node
289-
* }
290-
*/
291-
292-
func connect(root *Node) *Node {
293-
if root == nil {
294-
return root
295-
}
296-
traversal(root.Left, root.Right)
297-
return root
298-
}
299-
300-
func traversal(left, right *Node) {
301-
if left == nil || right == nil {
302-
return
303-
}
304-
left.Next = right
305-
traversal(left.Left, left.Right)
306-
traversal(left.Right, right.Left)
307-
traversal(right.Left, right.Right)
308-
}
309-
```
310-
311471
### **...**
312472

313473
```

0 commit comments

Comments
 (0)