diff --git a/README.md b/README.md index fb01fe8277a5a..a01a96597a1b2 100644 --- a/README.md +++ b/README.md @@ -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) ### 数学 diff --git a/README_EN.md b/README_EN.md index fda90683fa94d..b249a9cd04c0e 100644 --- a/README_EN.md +++ b/README_EN.md @@ -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 diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md index be6d53dd70616..5dc8bacf1eced 100644 --- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md +++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md @@ -50,11 +50,12 @@ struct Node {
  • -1000 <= node.val <= 1000
  • - ## 解法 +“BFS 层次遍历”实现。 + ### **Python3** @@ -62,7 +63,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 ``` ### **Java** @@ -70,7 +97,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/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:

    - - - -

     

    Example 1:

    - -

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

     

    Constraints:

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