Skip to content

Commit 59a4e2d

Browse files
solves #117 in java
1 parent 9379599 commit 59a4e2d

4 files changed

+65
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii) | [![Java](assets/java.png)](src/PathSumII.java) | |
109109
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list) | [![Java](assets/java.png)](src/FlattenBinaryTreeToLinkedList.java) | |
110110
| 116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node) | [![Java](assets/java.png)](src/PopulatingNextRightPointersInEachNode.java) | |
111-
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii) | | |
111+
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii) | [![Java](assets/java.png)](src/PopulatingNextRightPointersInEachNodeII.java) | |
112112
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle) | [![Java](assets/java.png)](src/PascalsTriangle.java) [![Python](assets/python.png)](python/pascals_triangle.py) | |
113113
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [![Java](assets/java.png)](src/PascalsTriangleII.java) [![Python](assets/python.png)](python/pascals_triangle_ii.py) | |
114114
| 120 | [Triangle](https://leetcode.com/problems/triangle) | | |

src/HelloWorld.java

-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
public class HelloWorld {
22
public static void main(String[] args) {
3-
PopulatingNextRightPointersInEachNode.Node root = new PopulatingNextRightPointersInEachNode.Node(1);
4-
root.left = new PopulatingNextRightPointersInEachNode.Node(2);
5-
root.right = new PopulatingNextRightPointersInEachNode.Node(3);
6-
root.left.left = new PopulatingNextRightPointersInEachNode.Node(4);
7-
root.left.right = new PopulatingNextRightPointersInEachNode.Node(5);
8-
root.right.left = new PopulatingNextRightPointersInEachNode.Node(6);
9-
root.right.right = new PopulatingNextRightPointersInEachNode.Node(7);
10-
11-
TreePrinter.print(root);
12-
13-
root = PopulatingNextRightPointersInEachNode.connect(root);
14-
15-
TreePrinter.print(root);
163
}
174
}

src/PopulatingNextRightPointersInEachNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static void addChildrenToQueue(Queue<Node> queue, Node root) {
3535
if (root.right != null) queue.add(root.right);
3636
}
3737

38-
public static class Node implements TreePrinter.PrintableNode {
38+
private static class Node implements TreePrinter.PrintableNode {
3939
public int val;
4040
public Node left;
4141
public Node right;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
public class PopulatingNextRightPointersInEachNodeII {
9+
public static Node connect(Node root) {
10+
if (root == null) return null;
11+
12+
final Queue<Node> queue = new LinkedList<>();
13+
queue.add(root);
14+
queue.add(null);
15+
Node previous = null;
16+
17+
while (!queue.isEmpty()) {
18+
final Node current = queue.poll();
19+
if (current == null) {
20+
previous = null;
21+
if (!queue.isEmpty()) queue.add(null);
22+
continue;
23+
}
24+
25+
if (previous != null) previous.next = current;
26+
previous = current;
27+
addChildrenToQueue(queue, current);
28+
}
29+
30+
return root;
31+
}
32+
33+
private static void addChildrenToQueue(Queue<Node> queue, Node root) {
34+
if (root.left != null) queue.add(root.left);
35+
if (root.right != null) queue.add(root.right);
36+
}
37+
38+
private static class Node implements TreePrinter.PrintableNode {
39+
public int val;
40+
public Node left;
41+
public Node right;
42+
public Node next;
43+
44+
public Node(int val) {
45+
this.val = val;
46+
}
47+
48+
@Override
49+
public TreePrinter.PrintableNode getLeft() {
50+
return left;
51+
}
52+
53+
@Override
54+
public TreePrinter.PrintableNode getRight() {
55+
return right;
56+
}
57+
58+
@Override
59+
public String getText() {
60+
return val + "";
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)