File tree 6 files changed +116
-21
lines changed
6 files changed +116
-21
lines changed Original file line number Diff line number Diff line change 107
107
| 112 | [ Path Sum] ( https://leetcode.com/problems/path-sum ) | [ ![ Java] ( assets/java.png )] ( src/PathSum.java ) [ ![ Python] ( assets/python.png )] ( python/path_sum.py ) | |
108
108
| 113 | [ Path Sum II] ( https://leetcode.com/problems/path-sum-ii ) | [ ![ Java] ( assets/java.png )] ( src/PathSumII.java ) | |
109
109
| 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 ) | |
111
111
| 117 | [ Populating Next Right Pointers in Each Node II] ( https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii ) | | |
112
112
| 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 ) | |
113
113
| 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 ) | |
Original file line number Diff line number Diff line change 1
1
public class HelloWorld {
2
2
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 );
9
10
10
11
TreePrinter .print (root );
11
12
12
- FlattenBinaryTreeToLinkedList . flatten (root );
13
+ root = PopulatingNextRightPointersInEachNode . connect (root );
13
14
14
15
TreePrinter .print (root );
15
-
16
16
}
17
17
}
Original file line number Diff line number Diff line change 1
1
import java .util .List ;
2
2
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 ;
6
7
7
- public Node () {}
8
+ public Node () {}
8
9
9
- public Node (int _val ) {
10
- val = _val ;
11
- }
10
+ public Node (int _val ) {
11
+ val = _val ;
12
+ }
12
13
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
+ }
16
18
}
17
- };
18
19
19
- public class MaximumDepthOfNAryTree {
20
20
public int maxDepth (Node root ) {
21
21
if (root == null ) return 0 ;
22
22
int maxDepth = 0 ;
Original file line number Diff line number Diff line change 2
2
import java .util .List ;
3
3
4
4
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
+
5
21
List <Integer > result = new ArrayList <>();
6
22
7
23
public List <Integer > preorder (Node root ) {
Original file line number Diff line number Diff line change 2
2
import java .util .List ;
3
3
4
4
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
+
5
21
List <Integer > result = new ArrayList <>();
6
22
7
23
public List <Integer > postorder (Node root ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments