Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9.1k

Commit c2456e3

Browse files
committedJun 1, 2021
feat: add solutions to lc problems: No.0116,0117
·
v0.3.0v0.1.4
1 parent 60dc630 commit c2456e3

File tree

12 files changed

+697
-86
lines changed

12 files changed

+697
-86
lines changed
 

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
- [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)
127127
- [将二叉搜索树转化为排序的双向链表](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README.md)
128128
- [二叉树的边界](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README.md)
129+
- [填充每个节点的下一个右侧节点指针](/solution/0100-0199/0116.Populating%20Next%20Right%20Pointers%20in%20Each%20Node/README.md)
130+
- [填充每个节点的下一个右侧节点指针 II](/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/README.md)
129131

130132
### 数学
131133

‎README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
122122
- [BiNode](/lcci/17.12.BiNode/README_EN.md)
123123
- [Convert Binary Search Tree to Sorted Doubly Linked List](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README_EN.md)
124124
- [Boundary of Binary Tree](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README_EN.md)
125+
- [Populating Next Right Pointers in Each Node](/solution/0100-0199/0116.Populating%20Next%20Right%20Pointers%20in%20Each%20Node/README_EN.md)
126+
- [Populating Next Right Pointers in Each Node II](/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/README_EN.md)
125127

126128
### Math
127129

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

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,149 @@ struct Node {
5050
<li><code>-1000 <= node.val <= 1000</code></li>
5151
</ul>
5252

53-
5453
## 解法
5554

5655
<!-- 这里可写通用的实现逻辑 -->
5756

57+
“BFS 层次遍历”实现。
58+
5859
<!-- tabs:start -->
5960

6061
### **Python3**
6162

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

6465
```python
65-
66+
"""
67+
# Definition for a Node.
68+
class Node:
69+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
70+
self.val = val
71+
self.left = left
72+
self.right = right
73+
self.next = next
74+
"""
75+
76+
class Solution:
77+
def connect(self, root: 'Node') -> 'Node':
78+
if root is None or (root.left is None and root.right is None):
79+
return root
80+
q = collections.deque([root])
81+
while q:
82+
size = len(q)
83+
cur = None
84+
for _ in range(size):
85+
node = q.popleft()
86+
if node.right:
87+
q.append(node.right)
88+
if node.left:
89+
q.append(node.left)
90+
node.next = cur
91+
cur = node
92+
return root
6693
```
6794

6895
### **Java**
6996

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

7299
```java
100+
/*
101+
// Definition for a Node.
102+
class Node {
103+
public int val;
104+
public Node left;
105+
public Node right;
106+
public Node next;
107+
108+
public Node() {}
109+
110+
public Node(int _val) {
111+
val = _val;
112+
}
113+
114+
public Node(int _val, Node _left, Node _right, Node _next) {
115+
val = _val;
116+
left = _left;
117+
right = _right;
118+
next = _next;
119+
}
120+
};
121+
*/
122+
123+
class Solution {
124+
public Node connect(Node root) {
125+
if (root == null || (root.left == null && root.right == null)) {
126+
return root;
127+
}
128+
Deque<Node> q = new ArrayDeque<>();
129+
q.offer(root);
130+
while (!q.isEmpty()) {
131+
Node cur = null;
132+
for (int i = 0, n = q.size(); i < n; ++i) {
133+
Node node = q.pollFirst();
134+
if (node.right != null) {
135+
q.offer(node.right);
136+
}
137+
if (node.left != null) {
138+
q.offer(node.left);
139+
}
140+
node.next = cur;
141+
cur = node;
142+
}
143+
}
144+
return root;
145+
}
146+
}
147+
```
73148

149+
### **C++**
150+
151+
```cpp
152+
/*
153+
// Definition for a Node.
154+
class Node {
155+
public:
156+
int val;
157+
Node* left;
158+
Node* right;
159+
Node* next;
160+
161+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
162+
163+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
164+
165+
Node(int _val, Node* _left, Node* _right, Node* _next)
166+
: val(_val), left(_left), right(_right), next(_next) {}
167+
};
168+
*/
169+
170+
class Solution {
171+
public:
172+
Node* connect(Node* root) {
173+
if (!root || (!root->left && !root->right)) {
174+
return root;
175+
}
176+
queue<Node*> q;
177+
q.push(root);
178+
while (!q.empty()) {
179+
Node* cur = nullptr;
180+
for (int i = 0, n = q.size(); i < n; ++i) {
181+
Node* node = q.front();
182+
q.pop();
183+
if (node->right) {
184+
q.push(node->right);
185+
}
186+
if (node->left) {
187+
q.push(node->left);
188+
}
189+
node->next = cur;
190+
cur = node;
191+
}
192+
}
193+
return root;
194+
}
195+
};
74196
```
75197
76198
### **...**

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

Lines changed: 122 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
<p>You are given a <strong>perfect binary tree</strong>&nbsp;where&nbsp;all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
88

9-
10-
119
<pre>
1210

1311
struct Node {
@@ -24,41 +22,25 @@ struct Node {
2422

2523
</pre>
2624

27-
28-
2925
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
3026

31-
32-
3327
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
3428

35-
36-
3729
<p>&nbsp;</p>
3830

39-
40-
4131
<p><strong>Follow up:</strong></p>
4232

43-
44-
4533
<ul>
4634
<li>You may only use constant extra space.</li>
4735
<li>Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.</li>
4836
</ul>
4937

50-
51-
5238
<p>&nbsp;</p>
5339

5440
<p><strong>Example 1:</strong></p>
5541

56-
57-
5842
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0116.Populating%20Next%20Right%20Pointers%20in%20Each%20Node/images/116_sample.png" style="width: 640px; height: 218px;" /></p>
5943

60-
61-
6244
<pre>
6345

6446
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
@@ -69,14 +51,10 @@ struct Node {
6951

7052
</pre>
7153

72-
73-
7454
<p>&nbsp;</p>
7555

7656
<p><strong>Constraints:</strong></p>
7757

78-
79-
8058
<ul>
8159
<li>The number of nodes in the given tree is less than <code>4096</code>.</li>
8260
<li><code>-1000 &lt;= node.val &lt;= 1000</code></li>
@@ -89,13 +67,134 @@ struct Node {
8967
### **Python3**
9068

9169
```python
92-
70+
"""
71+
# Definition for a Node.
72+
class Node:
73+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
74+
self.val = val
75+
self.left = left
76+
self.right = right
77+
self.next = next
78+
"""
79+
80+
class Solution:
81+
def connect(self, root: 'Node') -> 'Node':
82+
if root is None or (root.left is None and root.right is None):
83+
return root
84+
q = collections.deque([root])
85+
while q:
86+
size = len(q)
87+
cur = None
88+
for _ in range(size):
89+
node = q.popleft()
90+
if node.right:
91+
q.append(node.right)
92+
if node.left:
93+
q.append(node.left)
94+
node.next = cur
95+
cur = node
96+
return root
9397
```
9498

9599
### **Java**
96100

97101
```java
102+
/*
103+
// Definition for a Node.
104+
class Node {
105+
public int val;
106+
public Node left;
107+
public Node right;
108+
public Node next;
109+
110+
public Node() {}
111+
112+
public Node(int _val) {
113+
val = _val;
114+
}
115+
116+
public Node(int _val, Node _left, Node _right, Node _next) {
117+
val = _val;
118+
left = _left;
119+
right = _right;
120+
next = _next;
121+
}
122+
};
123+
*/
124+
125+
class Solution {
126+
public Node connect(Node root) {
127+
if (root == null || (root.left == null && root.right == null)) {
128+
return root;
129+
}
130+
Deque<Node> q = new ArrayDeque<>();
131+
q.offer(root);
132+
while (!q.isEmpty()) {
133+
Node cur = null;
134+
for (int i = 0, n = q.size(); i < n; ++i) {
135+
Node node = q.pollFirst();
136+
if (node.right != null) {
137+
q.offer(node.right);
138+
}
139+
if (node.left != null) {
140+
q.offer(node.left);
141+
}
142+
node.next = cur;
143+
cur = node;
144+
}
145+
}
146+
return root;
147+
}
148+
}
149+
```
98150

151+
### **C++**
152+
153+
```cpp
154+
/*
155+
// Definition for a Node.
156+
class Node {
157+
public:
158+
int val;
159+
Node* left;
160+
Node* right;
161+
Node* next;
162+
163+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
164+
165+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
166+
167+
Node(int _val, Node* _left, Node* _right, Node* _next)
168+
: val(_val), left(_left), right(_right), next(_next) {}
169+
};
170+
*/
171+
172+
class Solution {
173+
public:
174+
Node* connect(Node* root) {
175+
if (!root || (!root->left && !root->right)) {
176+
return root;
177+
}
178+
queue<Node*> q;
179+
q.push(root);
180+
while (!q.empty()) {
181+
Node* cur = nullptr;
182+
for (int i = 0, n = q.size(); i < n; ++i) {
183+
Node* node = q.front();
184+
q.pop();
185+
if (node->right) {
186+
q.push(node->right);
187+
}
188+
if (node->left) {
189+
q.push(node->left);
190+
}
191+
node->next = cur;
192+
cur = node;
193+
}
194+
}
195+
return root;
196+
}
197+
};
99198
```
100199
101200
### **...**

‎solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
Node* next;
9+
10+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
11+
12+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
13+
14+
Node(int _val, Node* _left, Node* _right, Node* _next)
15+
: val(_val), left(_left), right(_right), next(_next) {}
16+
};
17+
*/
18+
119
class Solution {
220
public:
321
Node* connect(Node* root) {
4-
if (!root) return nullptr;
22+
if (!root || (!root->left && !root->right)) {
23+
return root;
24+
}
525
queue<Node*> q;
626
q.push(root);
727
while (!q.empty()) {
8-
int size = q.size();
9-
for (int i = 0; i < size; ++i) {
10-
Node* t = q.front();
28+
Node* cur = nullptr;
29+
for (int i = 0, n = q.size(); i < n; ++i) {
30+
Node* node = q.front();
1131
q.pop();
12-
if (i < size - 1) {
13-
t->next = q.front();
32+
if (node->right) {
33+
q.push(node->right);
34+
}
35+
if (node->left) {
36+
q.push(node->left);
1437
}
15-
if (t->left) q.push(t->left);
16-
if (t->right) q.push(t->right);
38+
node->next = cur;
39+
cur = node;
1740
}
1841
}
1942
return root;
Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node left;
6+
public Node right;
7+
public Node next;
8+
9+
public Node() {}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
}
14+
15+
public Node(int _val, Node _left, Node _right, Node _next) {
16+
val = _val;
17+
left = _left;
18+
right = _right;
19+
next = _next;
20+
}
21+
};
22+
*/
23+
124
class Solution {
2-
public void connect(TreeLinkNode root) {
3-
if (root == null || root.left == null) return;
4-
root.left.next = root.right;
5-
if (root.next == null) root.right.next = null;
6-
else root.right.next = root.next.left;
7-
connect(root.left);
8-
connect(root.right);
25+
public Node connect(Node root) {
26+
if (root == null || (root.left == null && root.right == null)) {
27+
return root;
28+
}
29+
Deque<Node> q = new ArrayDeque<>();
30+
q.offer(root);
31+
while (!q.isEmpty()) {
32+
Node cur = null;
33+
for (int i = 0, n = q.size(); i < n; ++i) {
34+
Node node = q.pollFirst();
35+
if (node.right != null) {
36+
q.offer(node.right);
37+
}
38+
if (node.left != null) {
39+
q.offer(node.left);
40+
}
41+
node.next = cur;
42+
cur = node;
43+
}
44+
}
45+
return root;
946
}
1047
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
self.next = next
9+
"""
10+
11+
class Solution:
12+
def connect(self, root: 'Node') -> 'Node':
13+
if root is None or (root.left is None and root.right is None):
14+
return root
15+
q = collections.deque([root])
16+
while q:
17+
size = len(q)
18+
cur = None
19+
for _ in range(size):
20+
node = q.popleft()
21+
if node.right:
22+
q.append(node.right)
23+
if node.left:
24+
q.append(node.left)
25+
node.next = cur
26+
cur = node
27+
return root

‎solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,46 @@ struct Node {
5454
<ul>
5555
</ul>
5656

57-
5857
## 解法
5958

6059
<!-- 这里可写通用的实现逻辑 -->
6160

61+
“BFS 层次遍历”实现。
62+
6263
<!-- tabs:start -->
6364

6465
### **Python3**
6566

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

6869
```python
70+
"""
71+
# Definition for a Node.
72+
class Node:
73+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
74+
self.val = val
75+
self.left = left
76+
self.right = right
77+
self.next = next
78+
"""
79+
80+
class Solution:
81+
def connect(self, root: 'Node') -> 'Node':
82+
if root is None or (root.left is None and root.right is None):
83+
return root
84+
q = collections.deque([root])
85+
while q:
86+
size = len(q)
87+
cur = None
88+
for _ in range(size):
89+
node = q.popleft()
90+
if node.right:
91+
q.append(node.right)
92+
if node.left:
93+
q.append(node.left)
94+
node.next = cur
95+
cur = node
96+
return root
6997

7098
```
7199

@@ -74,7 +102,102 @@ struct Node {
74102
<!-- 这里可写当前语言的特殊实现逻辑 -->
75103

76104
```java
105+
/*
106+
// Definition for a Node.
107+
class Node {
108+
public int val;
109+
public Node left;
110+
public Node right;
111+
public Node next;
112+
113+
public Node() {}
114+
115+
public Node(int _val) {
116+
val = _val;
117+
}
118+
119+
public Node(int _val, Node _left, Node _right, Node _next) {
120+
val = _val;
121+
left = _left;
122+
right = _right;
123+
next = _next;
124+
}
125+
};
126+
*/
127+
128+
class Solution {
129+
public Node connect(Node root) {
130+
if (root == null || (root.left == null && root.right == null)) {
131+
return root;
132+
}
133+
Deque<Node> q = new ArrayDeque<>();
134+
q.offer(root);
135+
while (!q.isEmpty()) {
136+
Node cur = null;
137+
for (int i = 0, n = q.size(); i < n; ++i) {
138+
Node node = q.pollFirst();
139+
if (node.right != null) {
140+
q.offer(node.right);
141+
}
142+
if (node.left != null) {
143+
q.offer(node.left);
144+
}
145+
node.next = cur;
146+
cur = node;
147+
}
148+
}
149+
return root;
150+
}
151+
}
152+
```
77153

154+
### **C++**
155+
156+
```cpp
157+
/*
158+
// Definition for a Node.
159+
class Node {
160+
public:
161+
int val;
162+
Node* left;
163+
Node* right;
164+
Node* next;
165+
166+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
167+
168+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
169+
170+
Node(int _val, Node* _left, Node* _right, Node* _next)
171+
: val(_val), left(_left), right(_right), next(_next) {}
172+
};
173+
*/
174+
175+
class Solution {
176+
public:
177+
Node* connect(Node* root) {
178+
if (!root || (!root->left && !root->right)) {
179+
return root;
180+
}
181+
queue<Node*> q;
182+
q.push(root);
183+
while (!q.empty()) {
184+
Node* cur = nullptr;
185+
for (int i = 0, n = q.size(); i < n; ++i) {
186+
Node* node = q.front();
187+
q.pop();
188+
if (node->right) {
189+
q.push(node->right);
190+
}
191+
if (node->left) {
192+
q.push(node->left);
193+
}
194+
node->next = cur;
195+
cur = node;
196+
}
197+
}
198+
return root;
199+
}
200+
};
78201
```
79202
80203
### **...**

‎solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md

Lines changed: 122 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
<p>Given a binary tree</p>
88

9-
10-
119
<pre>
1210

1311
struct Node {
@@ -24,30 +22,19 @@ struct Node {
2422

2523
</pre>
2624

27-
28-
2925
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
3026

31-
32-
3327
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
3428

35-
36-
3729
<p>&nbsp;</p>
3830

39-
40-
4131
<p><strong>Follow up:</strong></p>
4232

43-
44-
4533
<ul>
4634
<li>You may only use constant extra space.</li>
4735
<li>Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.</li>
4836
</ul>
4937

50-
5138
<p>&nbsp;</p>
5239
<p><strong>Example 1:</strong></p>
5340

@@ -67,21 +54,142 @@ struct Node {
6754
<li><code>-100&nbsp;&lt;= node.val &lt;= 100</code></li>
6855
</ul>
6956

70-
7157
## Solutions
7258

7359
<!-- tabs:start -->
7460

7561
### **Python3**
7662

7763
```python
64+
"""
65+
# Definition for a Node.
66+
class Node:
67+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
68+
self.val = val
69+
self.left = left
70+
self.right = right
71+
self.next = next
72+
"""
73+
74+
class Solution:
75+
def connect(self, root: 'Node') -> 'Node':
76+
if root is None or (root.left is None and root.right is None):
77+
return root
78+
q = collections.deque([root])
79+
while q:
80+
size = len(q)
81+
cur = None
82+
for _ in range(size):
83+
node = q.popleft()
84+
if node.right:
85+
q.append(node.right)
86+
if node.left:
87+
q.append(node.left)
88+
node.next = cur
89+
cur = node
90+
return root
7891

7992
```
8093

8194
### **Java**
8295

8396
```java
97+
/*
98+
// Definition for a Node.
99+
class Node {
100+
public int val;
101+
public Node left;
102+
public Node right;
103+
public Node next;
104+
105+
public Node() {}
106+
107+
public Node(int _val) {
108+
val = _val;
109+
}
110+
111+
public Node(int _val, Node _left, Node _right, Node _next) {
112+
val = _val;
113+
left = _left;
114+
right = _right;
115+
next = _next;
116+
}
117+
};
118+
*/
119+
120+
class Solution {
121+
public Node connect(Node root) {
122+
if (root == null || (root.left == null && root.right == null)) {
123+
return root;
124+
}
125+
Deque<Node> q = new ArrayDeque<>();
126+
q.offer(root);
127+
while (!q.isEmpty()) {
128+
Node cur = null;
129+
for (int i = 0, n = q.size(); i < n; ++i) {
130+
Node node = q.pollFirst();
131+
if (node.right != null) {
132+
q.offer(node.right);
133+
}
134+
if (node.left != null) {
135+
q.offer(node.left);
136+
}
137+
node.next = cur;
138+
cur = node;
139+
}
140+
}
141+
return root;
142+
}
143+
}
144+
```
84145

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+
Node* next;
157+
158+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
159+
160+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
161+
162+
Node(int _val, Node* _left, Node* _right, Node* _next)
163+
: val(_val), left(_left), right(_right), next(_next) {}
164+
};
165+
*/
166+
167+
class Solution {
168+
public:
169+
Node* connect(Node* root) {
170+
if (!root || (!root->left && !root->right)) {
171+
return root;
172+
}
173+
queue<Node*> q;
174+
q.push(root);
175+
while (!q.empty()) {
176+
Node* cur = nullptr;
177+
for (int i = 0, n = q.size(); i < n; ++i) {
178+
Node* node = q.front();
179+
q.pop();
180+
if (node->right) {
181+
q.push(node->right);
182+
}
183+
if (node->left) {
184+
q.push(node->left);
185+
}
186+
node->next = cur;
187+
cur = node;
188+
}
189+
}
190+
return root;
191+
}
192+
};
85193
```
86194
87195
### **...**
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* left;
7+
Node* right;
8+
Node* next;
9+
10+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
11+
12+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
13+
14+
Node(int _val, Node* _left, Node* _right, Node* _next)
15+
: val(_val), left(_left), right(_right), next(_next) {}
16+
};
17+
*/
18+
119
class Solution {
220
public:
321
Node* connect(Node* root) {
4-
if (!root) return nullptr;
22+
if (!root || (!root->left && !root->right)) {
23+
return root;
24+
}
525
queue<Node*> q;
626
q.push(root);
727
while (!q.empty()) {
8-
int size = q.size();
9-
for (int i = 0; i < size; ++i) {
10-
Node* t = q.front();
28+
Node* cur = nullptr;
29+
for (int i = 0, n = q.size(); i < n; ++i) {
30+
Node* node = q.front();
1131
q.pop();
12-
if (i < size - 1) {
13-
t->next = q.front();
32+
if (node->right) {
33+
q.push(node->right);
1434
}
15-
if (t->left) q.push(t->left);
16-
if (t->right) q.push(t->right);
35+
if (node->left) {
36+
q.push(node->left);
37+
}
38+
node->next = cur;
39+
cur = node;
1740
}
1841
}
19-
2042
return root;
2143
}
2244
};
Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
public class Solution {
2-
public void connect(TreeLinkNode root) {
3-
if (root == null) return;
4-
TreeLinkNode first_node_next_layer = null;
5-
TreeLinkNode preNode = null;
6-
for (TreeLinkNode curNode = root; curNode != null; curNode = curNode.next) {
7-
if (curNode.left != null) {
8-
if (preNode == null) {
9-
preNode = curNode.left;
10-
first_node_next_layer = curNode.left;
11-
} else {
12-
preNode.next = curNode.left;
13-
preNode = preNode.next;
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node left;
6+
public Node right;
7+
public Node next;
8+
9+
public Node() {}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
}
14+
15+
public Node(int _val, Node _left, Node _right, Node _next) {
16+
val = _val;
17+
left = _left;
18+
right = _right;
19+
next = _next;
20+
}
21+
};
22+
*/
23+
24+
class Solution {
25+
public Node connect(Node root) {
26+
if (root == null || (root.left == null && root.right == null)) {
27+
return root;
28+
}
29+
Deque<Node> q = new ArrayDeque<>();
30+
q.offer(root);
31+
while (!q.isEmpty()) {
32+
Node cur = null;
33+
for (int i = 0, n = q.size(); i < n; ++i) {
34+
Node node = q.pollFirst();
35+
if (node.right != null) {
36+
q.offer(node.right);
1437
}
15-
}
16-
if (curNode.right != null) {
17-
if (preNode == null) {
18-
preNode = curNode.right;
19-
first_node_next_layer = curNode.right;
20-
} else {
21-
preNode.next = curNode.right;
22-
preNode = preNode.next;
38+
if (node.left != null) {
39+
q.offer(node.left);
2340
}
41+
node.next = cur;
42+
cur = node;
2443
}
2544
}
26-
connect(first_node_next_layer);
45+
return root;
2746
}
2847
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
self.next = next
9+
"""
10+
11+
class Solution:
12+
def connect(self, root: 'Node') -> 'Node':
13+
if root is None or (root.left is None and root.right is None):
14+
return root
15+
q = collections.deque([root])
16+
while q:
17+
size = len(q)
18+
cur = None
19+
for _ in range(size):
20+
node = q.popleft()
21+
if node.right:
22+
q.append(node.right)
23+
if node.left:
24+
q.append(node.left)
25+
node.next = cur
26+
cur = node
27+
return root

0 commit comments

Comments
 (0)
Please sign in to comment.