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;
+ }
+};
```
### **...**
diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md
index b60e85e813377..bb55c9d4dc9b9 100644
--- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md
+++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md
@@ -6,8 +6,6 @@
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
-
-
struct Node {
@@ -24,41 +22,25 @@ struct Node {
-
-
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 NULL
.
-
-
Initially, all next pointers are set to NULL
.
-
-
-
-
Follow up:
-
-
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
-
-
Example 1:
-
-

-
-
Input: root = [1,2,3,4,5,6,7]
@@ -69,14 +51,10 @@ struct Node {
-
-
Constraints:
-
-
- The number of nodes in the given tree is less than
4096
.
-1000 <= node.val <= 1000
@@ -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 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 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;
+ }
+};
```
### **...**
diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp
index 97bed5f8756cd..328ab4fe61758 100644
--- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp
+++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp
@@ -1,19 +1,42 @@
+/*
+// 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) return nullptr;
+ if (!root || (!root->left && !root->right)) {
+ return root;
+ }
queue q;
q.push(root);
while (!q.empty()) {
- int size = q.size();
- for (int i = 0; i < size; ++i) {
- Node* t = q.front();
+ Node* cur = nullptr;
+ for (int i = 0, n = q.size(); i < n; ++i) {
+ Node* node = q.front();
q.pop();
- if (i < size - 1) {
- t->next = q.front();
+ if (node->right) {
+ q.push(node->right);
+ }
+ if (node->left) {
+ q.push(node->left);
}
- if (t->left) q.push(t->left);
- if (t->right) q.push(t->right);
+ node->next = cur;
+ cur = node;
}
}
return root;
diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.java b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.java
index a289983691a74..bb0114e371ef6 100644
--- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.java
+++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.java
@@ -1,10 +1,47 @@
+/*
+// 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 void connect(TreeLinkNode root) {
- if (root == null || root.left == null) return;
- root.left.next = root.right;
- if (root.next == null) root.right.next = null;
- else root.right.next = root.next.left;
- connect(root.left);
- connect(root.right);
+ public Node connect(Node root) {
+ if (root == null || (root.left == null && root.right == null)) {
+ return root;
+ }
+ Deque 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;
}
}
\ No newline at end of file
diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py
new file mode 100644
index 0000000000000..794ab2d74053b
--- /dev/null
+++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py
@@ -0,0 +1,27 @@
+"""
+# 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
diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md
index 5055ce0c6cfc9..db2f646667508 100644
--- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md
+++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md
@@ -54,11 +54,12 @@ struct Node {
-
## 解法
+“BFS 层次遍历”实现。
+
### **Python3**
@@ -66,6 +67,33 @@ struct Node {
```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
```
@@ -74,7 +102,102 @@ struct Node {
```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 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 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;
+ }
+};
```
### **...**
diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md
index 7d9f09609741b..2c903a3e1da27 100644
--- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md
+++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md
@@ -6,8 +6,6 @@
Given a binary tree
-
-
struct Node {
@@ -24,30 +22,19 @@ struct Node {
-
-
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 NULL
.
-
-
Initially, all next pointers are set to NULL
.
-
-
-
-
Follow up:
-
-
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
-
Example 1:
@@ -67,7 +54,6 @@ struct Node {
-100 <= node.val <= 100
-
## Solutions
@@ -75,13 +61,135 @@ 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 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 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;
+ }
+};
```
### **...**
diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.cpp b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.cpp
index 89042ad88f31c..328ab4fe61758 100644
--- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.cpp
+++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.cpp
@@ -1,22 +1,44 @@
+/*
+// 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) return nullptr;
+ if (!root || (!root->left && !root->right)) {
+ return root;
+ }
queue q;
q.push(root);
while (!q.empty()) {
- int size = q.size();
- for (int i = 0; i < size; ++i) {
- Node* t = q.front();
+ Node* cur = nullptr;
+ for (int i = 0, n = q.size(); i < n; ++i) {
+ Node* node = q.front();
q.pop();
- if (i < size - 1) {
- t->next = q.front();
+ if (node->right) {
+ q.push(node->right);
}
- if (t->left) q.push(t->left);
- if (t->right) q.push(t->right);
+ if (node->left) {
+ q.push(node->left);
+ }
+ node->next = cur;
+ cur = node;
}
}
-
return root;
}
};
\ No newline at end of file
diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.java b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.java
index 0961a1d1999de..bb0114e371ef6 100644
--- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.java
+++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.java
@@ -1,28 +1,47 @@
-public class Solution {
- public void connect(TreeLinkNode root) {
- if (root == null) return;
- TreeLinkNode first_node_next_layer = null;
- TreeLinkNode preNode = null;
- for (TreeLinkNode curNode = root; curNode != null; curNode = curNode.next) {
- if (curNode.left != null) {
- if (preNode == null) {
- preNode = curNode.left;
- first_node_next_layer = curNode.left;
- } else {
- preNode.next = curNode.left;
- preNode = preNode.next;
+/*
+// 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 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 (curNode.right != null) {
- if (preNode == null) {
- preNode = curNode.right;
- first_node_next_layer = curNode.right;
- } else {
- preNode.next = curNode.right;
- preNode = preNode.next;
+ if (node.left != null) {
+ q.offer(node.left);
}
+ node.next = cur;
+ cur = node;
}
}
- connect(first_node_next_layer);
+ return root;
}
}
\ No newline at end of file
diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py
new file mode 100644
index 0000000000000..794ab2d74053b
--- /dev/null
+++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py
@@ -0,0 +1,27 @@
+"""
+# 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