Skip to content

Commit 58c52e5

Browse files
committed
finish "117. Populating Next Right Pointers in Each Node II". Fix #117
1 parent 82df772 commit 58c52e5

File tree

6 files changed

+159
-36
lines changed

6 files changed

+159
-36
lines changed

README.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,11 @@
706706
|{doc_base_url}/0116-populating-next-right-pointers-in-each-node.adoc[Note]
707707
|Medium
708708

709-
//|117
710-
//|{leetcode_base_url}/populating-next-right-pointers-in-each-node-ii/[Populating Next Right Pointers in Each Node II]
711-
//|{source_base_url}/_0117_PopulatingNextRightPointersInEachNodeII.java[Java]
712-
//|{doc_base_url}/0117-populating-next-right-pointers-in-each-node-ii.adoc[Note]
713-
//|Medium
709+
|117
710+
|{leetcode_base_url}/populating-next-right-pointers-in-each-node-ii/[Populating Next Right Pointers in Each Node II]
711+
|{source_base_url}/_0117_PopulatingNextRightPointersInEachNodeII.java[Java]
712+
|{doc_base_url}/0117-populating-next-right-pointers-in-each-node-ii.adoc[Note]
713+
|Medium
714714

715715
|118
716716
|{leetcode_base_url}/pascals-triangle/[Pascal's Triangle]

docs/0116-populating-next-right-pointers-in-each-node.adoc

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,9 @@
22

33
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/[LeetCode - Populating Next Right Pointers in Each Node]
44

5-
image::images/0116-0.png[]
6-
7-
image::images/0116-1.jpg[]
8-
9-
image::images/0116-2.jpg[]
10-
11-
这道题的关键是在上层遍历中,把下层的链接关系建立起来。
12-
13-
因为是完全二叉树。所以,如果左下节点为空则到达最后一层;向右节点为空,则到达行尾需要换行。
14-
15-
=== 参考资料
16-
17-
. https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--27/[详细通俗的思路分析,多解法 - 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)]
18-
195
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:
206

7+
[source,c]
218
[subs="verbatim,quotes"]
229
----
2310
struct Node {
@@ -32,32 +19,42 @@ Populate each next pointer to point to its next right node. If there is no next
3219

3320
Initially, all next pointers are set to `NULL`.
3421

35-
36-
3722
*Follow up:*
3823

39-
4024
* You may only use constant extra space.
4125
* Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
4226
4327
44-
28+
4529
*Example 1:*
4630

47-
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 640px; height: 218px;" />
31+
image::images/0116-0.png[]
4832

4933
[subs="verbatim,quotes"]
5034
----
5135
*Input:* root = [1,2,3,4,5,6,7]
5236
*Output:* [1,#,2,3,#,4,5,6,7,#]
53-
*Explanation: *Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
37+
*Explanation:* Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
5438
----
5539

56-
5740
*Constraints:*
5841

59-
6042
* The number of nodes in the given tree is less than `4096`.
6143
* `-1000 <= node.val <= 1000`
6244
45+
=== 解题分析
46+
47+
这道题和 xref:0117-populating-next-right-pointers-in-each-node-ii.adoc[117. Populating Next Right Pointers in Each Node II] 算是姊妹题。
48+
49+
image::images/0116-1.jpg[]
50+
51+
image::images/0116-2.jpg[]
52+
53+
这道题的关键是在上层遍历中,把下层的链接关系建立起来。
54+
55+
因为是完全二叉树。所以,如果左下节点为空则到达最后一层;向右节点为空,则到达行尾需要换行。
56+
57+
=== 参考资料
58+
59+
. https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--27/[详细通俗的思路分析,多解法 - 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)]
6360

docs/0117-populating-next-right-pointers-in-each-node-ii.adoc

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/[Le
44

55
Given a binary tree
66

7+
[source,c]
78
[subs="verbatim,quotes"]
89
----
910
struct Node {
@@ -18,32 +19,44 @@ Populate each next pointer to point to its next right node. If there is no next
1819

1920
Initially, all next pointers are set to `NULL`.
2021

21-
22-
2322
*Follow up:*
2423

25-
2624
* You may only use constant extra space.
2725
* Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
2826
29-
30-
3127
*Example 1:*
3228

33-
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 640px; height: 218px;" />
29+
image::images/0117-1.png[]
3430

3531
[subs="verbatim,quotes"]
3632
----
3733
*Input:* root = [1,2,3,4,5,null,7]
3834
*Output:* [1,#,2,3,#,4,5,7,#]
39-
*Explanation: *Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
35+
*Explanation:* Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
4036
----
4137

42-
4338
*Constraints:*
4439

45-
4640
* The number of nodes in the given tree is less than `6000`.
4741
* `-100 <= node.val <= 100`
4842
4943
44+
=== 解题分析
45+
46+
这道题和 xref:0116-populating-next-right-pointers-in-each-node.adoc[116. Populating Next Right Pointers in Each Node] 算是姊妹题。
47+
48+
最简单的方式,使用 `Deque` 来保存每一层节点,然后建立起来"连接"。但是,很明显,这种方案不符合空间复杂度要求。
49+
50+
基于上面这种解法,再深入思考一步,上面使用 `Deque` 就是想要保存接下来需要访问的元素,并且保存访问的前后顺序。现在 `Node` 上有 `next` 字段,可以利用这个字段,打通这条链表,遍历上一层时,打通下一次的链接结构。这里需要保存的就有两点:
51+
52+
. 这条链表的头结点,用于下一层的遍历;
53+
. 这条链表的尾节点,用于添加下一个节点。
54+
55+
image::images/0117-2.jpg[]
56+
57+
这样,把第一种解法的代码稍作修改就可以了。
58+
59+
=== 参考资料
60+
61+
. https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-28/[详细通俗的思路分析,多解法 - 填充每个节点的下一个右侧节点指针 II - 力扣(LeetCode)]
62+
. https://leetcode.wang/leetcode-117-Populating-Next-Right-Pointers-in-Each-NodeII.html[117. Populating Next Right Pointers in Each Node II · leetcode]

docs/images/0117-1.png

29.9 KB
Loading

docs/images/0117-2.jpg

12.7 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import java.util.Deque;
6+
import java.util.LinkedList;
7+
import java.util.Objects;
8+
9+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
10+
import static java.util.Arrays.asList;
11+
12+
/**
13+
* @author D瓜哥, https://www.diguage.com/
14+
* @since 2020-02-09 23:17
15+
*/
16+
public class _0117_PopulatingNextRightPointersInEachNodeII {
17+
18+
static class Node {
19+
public int val;
20+
public Node left;
21+
public Node right;
22+
public Node next;
23+
24+
public Node(int x) {
25+
this.val = x;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Node{" +
31+
"val=" + val +
32+
", left=" + left +
33+
", right=" + right +
34+
'}';
35+
}
36+
}
37+
38+
39+
/**
40+
* Runtime: 1 ms, faster than 49.14% of Java online submissions for Populating Next Right Pointers in Each Node II.
41+
* Memory Usage: 41.4 MB, less than 100.00% of Java online submissions for Populating Next Right Pointers in Each Node II.
42+
*
43+
* Copy from: https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-28/[详细通俗的思路分析,多解法 - 填充每个节点的下一个右侧节点指针 II - 力扣(LeetCode)]
44+
*/
45+
public Node connect(Node root) {
46+
Node curr = root;
47+
while (Objects.nonNull(curr)) {
48+
Node dummy = new Node(0);
49+
Node tail = dummy;
50+
while (Objects.nonNull(curr)) {
51+
if (Objects.nonNull(curr.left)) {
52+
tail.next = curr.left;
53+
tail = tail.next;
54+
}
55+
if (Objects.nonNull(curr.right)) {
56+
tail.next = curr.right;
57+
tail = tail.next;
58+
}
59+
curr = curr.next;
60+
}
61+
curr = dummy.next;
62+
}
63+
return root;
64+
}
65+
66+
/**
67+
* Runtime: 1 ms, faster than 49.14% of Java online submissions for Populating Next Right Pointers in Each Node II.
68+
* Memory Usage: 40.8 MB, less than 100.00% of Java online submissions for Populating Next Right Pointers in Each Node II.
69+
*/
70+
public Node connectDeque(Node root) {
71+
if (Objects.isNull(root)) {
72+
return null;
73+
}
74+
Deque<Node> deque = new LinkedList<>();
75+
deque.addLast(root);
76+
Node prev = null;
77+
while (!deque.isEmpty()) {
78+
int size = deque.size();
79+
for (int i = 0; i < size; i++) {
80+
Node curr = deque.removeFirst();
81+
if (Objects.nonNull(curr.left)) {
82+
deque.addLast(curr.left);
83+
}
84+
if (Objects.nonNull(curr.right)) {
85+
deque.addLast(curr.right);
86+
}
87+
if (i > 0) {
88+
prev.next = curr;
89+
}
90+
prev = curr;
91+
}
92+
}
93+
return root;
94+
}
95+
96+
public static void main(String[] args) {
97+
_0117_PopulatingNextRightPointersInEachNodeII solution = new _0117_PopulatingNextRightPointersInEachNodeII();
98+
TreeNode treeNode = buildTree(asList(1, 2, 3, 4, 5, null, 7));
99+
Node node = convert(treeNode);
100+
Node r1 = solution.connect(node);
101+
System.out.println(r1);
102+
}
103+
104+
private static Node convert(TreeNode treeNode) {
105+
if (Objects.isNull(treeNode)) {
106+
return null;
107+
}
108+
Node node = new Node(treeNode.val);
109+
node.left = convert(treeNode.left);
110+
node.right = convert(treeNode.right);
111+
return node;
112+
}
113+
}

0 commit comments

Comments
 (0)