Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.0116,0117
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jun 1, 2021
1 parent 60dc630 commit c2456e3
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 86 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
- [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)
- [将二叉搜索树转化为排序的双向链表](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README.md)
- [二叉树的边界](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README.md)
- [填充每个节点的下一个右侧节点指针](/solution/0100-0199/0116.Populating%20Next%20Right%20Pointers%20in%20Each%20Node/README.md)
- [填充每个节点的下一个右侧节点指针 II](/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/README.md)

### 数学

Expand Down
2 changes: 2 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
- [BiNode](/lcci/17.12.BiNode/README_EN.md)
- [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)
- [Boundary of Binary Tree](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README_EN.md)
- [Populating Next Right Pointers in Each Node](/solution/0100-0199/0116.Populating%20Next%20Right%20Pointers%20in%20Each%20Node/README_EN.md)
- [Populating Next Right Pointers in Each Node II](/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/README_EN.md)

### Math

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,149 @@ struct Node {
<li><code>-1000 <= node.val <= 1000</code></li>
</ul>


## 解法

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

“BFS 层次遍历”实现。

<!-- tabs:start -->

### **Python3**

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

```python

"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""

class Solution:
def connect(self, root: 'Node') -> 'Node':
if root is None or (root.left is None and root.right is None):
return root
q = collections.deque([root])
while q:
size = len(q)
cur = None
for _ in range(size):
node = q.popleft()
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
node.next = cur
cur = node
return root
```

### **Java**

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

```java
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/

class Solution {
public Node connect(Node root) {
if (root == null || (root.left == null && root.right == null)) {
return root;
}
Deque<Node> q = new ArrayDeque<>();
q.offer(root);
while (!q.isEmpty()) {
Node cur = null;
for (int i = 0, n = q.size(); i < n; ++i) {
Node node = q.pollFirst();
if (node.right != null) {
q.offer(node.right);
}
if (node.left != null) {
q.offer(node.left);
}
node.next = cur;
cur = node;
}
}
return root;
}
}
```

### **C++**

```cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
Node* connect(Node* root) {
if (!root || (!root->left && !root->right)) {
return root;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* cur = nullptr;
for (int i = 0, n = q.size(); i < n; ++i) {
Node* node = q.front();
q.pop();
if (node->right) {
q.push(node->right);
}
if (node->left) {
q.push(node->left);
}
node->next = cur;
cur = node;
}
}
return root;
}
};
```
### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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



<pre>

struct Node {
Expand All @@ -24,41 +22,25 @@ struct Node {

</pre>



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



<p>Initially, all next pointers are set to <code>NULL</code>.</p>



<p>&nbsp;</p>



<p><strong>Follow up:</strong></p>



<ul>
<li>You may only use constant extra space.</li>
<li>Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.</li>
</ul>



<p>&nbsp;</p>

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



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



<pre>

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

</pre>



<p>&nbsp;</p>

<p><strong>Constraints:</strong></p>



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

```python

"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""

class Solution:
def connect(self, root: 'Node') -> 'Node':
if root is None or (root.left is None and root.right is None):
return root
q = collections.deque([root])
while q:
size = len(q)
cur = None
for _ in range(size):
node = q.popleft()
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
node.next = cur
cur = node
return root
```

### **Java**

```java
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/

class Solution {
public Node connect(Node root) {
if (root == null || (root.left == null && root.right == null)) {
return root;
}
Deque<Node> q = new ArrayDeque<>();
q.offer(root);
while (!q.isEmpty()) {
Node cur = null;
for (int i = 0, n = q.size(); i < n; ++i) {
Node node = q.pollFirst();
if (node.right != null) {
q.offer(node.right);
}
if (node.left != null) {
q.offer(node.left);
}
node.next = cur;
cur = node;
}
}
return root;
}
}
```

### **C++**

```cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
Node* connect(Node* root) {
if (!root || (!root->left && !root->right)) {
return root;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* cur = nullptr;
for (int i = 0, n = q.size(); i < n; ++i) {
Node* node = q.front();
q.pop();
if (node->right) {
q.push(node->right);
}
if (node->left) {
q.push(node->left);
}
node->next = cur;
cur = node;
}
}
return root;
}
};
```
### **...**
Expand Down
Loading

0 comments on commit c2456e3

Please sign in to comment.