Skip to content

Commit c2456e3

Browse files
committed
feat: add solutions to lc problems: No.0116,0117
1 parent 60dc630 commit c2456e3

File tree

12 files changed

+697
-86
lines changed

12 files changed

+697
-86
lines changed

README.md

+2
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

+2
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

+124-2
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

+122-23
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
### **...**

0 commit comments

Comments
 (0)