Skip to content

Commit 9379599

Browse files
committedMay 30, 2022
solves #114 in java
1 parent d307847 commit 9379599

6 files changed

+116
-21
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | [![Java](assets/java.png)](src/PathSum.java) [![Python](assets/python.png)](python/path_sum.py) | |
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) | |
110-
| 116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node) | | |
110+
| 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) | |
111111
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii) | | |
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) | |

‎src/HelloWorld.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
public class HelloWorld {
22
public static void main(String[] args) {
3-
TreeNode root = new TreeNode(1);
4-
root.left = new TreeNode(2);
5-
root.right = new TreeNode(5);
6-
root.left.left = new TreeNode(3);
7-
root.left.right = new TreeNode(4);
8-
root.right.right = new TreeNode(6);
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);
910

1011
TreePrinter.print(root);
1112

12-
FlattenBinaryTreeToLinkedList.flatten(root);
13+
root = PopulatingNextRightPointersInEachNode.connect(root);
1314

1415
TreePrinter.print(root);
15-
1616
}
1717
}

‎src/MaximumDepthOfNAryTree.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import java.util.List;
22

3-
class Node {
4-
public int val;
5-
public List<Node> children;
3+
public class MaximumDepthOfNAryTree {
4+
private static class Node {
5+
public int val;
6+
public List<Node> children;
67

7-
public Node() {}
8+
public Node() {}
89

9-
public Node(int _val) {
10-
val = _val;
11-
}
10+
public Node(int _val) {
11+
val = _val;
12+
}
1213

13-
public Node(int _val, List<Node> _children) {
14-
val = _val;
15-
children = _children;
14+
public Node(int _val, List<Node> _children) {
15+
val = _val;
16+
children = _children;
17+
}
1618
}
17-
};
1819

19-
public class MaximumDepthOfNAryTree {
2020
public int maxDepth(Node root) {
2121
if (root == null) return 0;
2222
int maxDepth = 0;

‎src/NArayTreePreOrderTraversal.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
import java.util.List;
33

44
public class NArayTreePreOrderTraversal {
5+
private static class Node {
6+
public int val;
7+
public List<Node> children;
8+
9+
public Node() {}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
}
14+
15+
public Node(int _val, List<Node> _children) {
16+
val = _val;
17+
children = _children;
18+
}
19+
}
20+
521
List<Integer> result = new ArrayList<>();
622

723
public List<Integer> preorder(Node root) {

‎src/NAryTreePostorderTraversal.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
import java.util.List;
33

44
public class NAryTreePostorderTraversal {
5+
private static class Node {
6+
public int val;
7+
public List<Node> children;
8+
9+
public Node() {}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
}
14+
15+
public Node(int _val, List<Node> _children) {
16+
val = _val;
17+
children = _children;
18+
}
19+
}
20+
521
List<Integer> result = new ArrayList<>();
622

723
public List<Integer> postorder(Node root) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
public class PopulatingNextRightPointersInEachNode {
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+
public 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)
Please sign in to comment.