File tree 2 files changed +62
-0
lines changed
2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ package medium ;
2
+ import classes .TreeLinkNode ;
3
+
4
+ /**
5
+ * Created by fishercoder1534 on 10/5/16.
6
+ */
7
+ public class PopulatingNextRightPointersinEachNode {
8
+ //copied this post: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
9
+ //very clever and concise to make it in O(1) space
10
+
11
+ //based on level order traversal
12
+ public static void connect (TreeLinkNode root ) {
13
+
14
+ TreeLinkNode head = null ; //head of the next level
15
+ TreeLinkNode prev = null ; //the leading node on the next level
16
+ TreeLinkNode cur = root ; //current node of current level
17
+
18
+ while (cur != null ) {
19
+
20
+ while (cur != null ) { //iterate on the current level
21
+ //left child
22
+ if (cur .left != null ) {
23
+ if (prev != null ) {
24
+ prev .next = cur .left ;
25
+ } else {
26
+ head = cur .left ;
27
+ }
28
+ prev = cur .left ;
29
+ }
30
+ //right child
31
+ if (cur .right != null ) {
32
+ if (prev != null ) {
33
+ prev .next = cur .right ;
34
+ } else {
35
+ head = cur .right ;
36
+ }
37
+ prev = cur .right ;
38
+ }
39
+ //move to next node
40
+ cur = cur .next ;
41
+ }
42
+
43
+ //move to next level
44
+ cur = head ;
45
+ head = null ;
46
+ prev = null ;
47
+ }
48
+
49
+ }
50
+
51
+ public static void main (String ...args ){
52
+ TreeLinkNode root = new TreeLinkNode (1 );
53
+ root .left = new TreeLinkNode (2 );
54
+ root .right = new TreeLinkNode (3 );
55
+ root .left .left = new TreeLinkNode (4 );
56
+ root .left .right = new TreeLinkNode (5 );
57
+ root .right .right = new TreeLinkNode (7 );
58
+ connect (root );
59
+ }
60
+ }
Original file line number Diff line number Diff line change 8
8
|140|[ Word Break II] ( https://leetcode.com/problems/word-break-ii/ ) |[ Solution] ( ../../blob/master/HARD/src/hard/WordBreakII.java ) | ? |O(n^2) | Hard| Backtracking/DFS
9
9
|139|[ Word Break] ( https://leetcode.com/problems/word-break/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/WordBreak.java ) | O(n^2)|O(n) | Medium| DP
10
10
|133|[ Clone Graph] ( https://leetcode.com/problems/clone-graph/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/CloneGraph.java ) | O(n)|O(n) | Medium| HashMap, BFS
11
+ |117|[ Populating Next Right Pointers in Each Node II] ( https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ ) |[ Solution] ( ../../blob/master/HARD/src/hard/PopulatingNextRightPointersinEachNodeII.java ) | O(n)|O(1) | Hard| BFS
12
+ |116|[ Populating Next Right Pointers in Each Node] ( https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/PopulatingNextRightPointersinEachNode.java ) | O(n)|O(1) | Medium| BFS
11
13
|91|[ Decode Ways] ( https://leetcode.com/problems/decode-ways/ ) |[ Solution] ( ../../blob/master/MEDIUM/src/medium/DecodeWays.java ) | O(n)|O(n) | Medium| DP
12
14
|76|[ Minimum Window Substring] ( https://leetcode.com/problems/minimum-window-substring/ ) |[ Solution] ( ../../blob/master/HARD/src/hard/MinimumWindowSubstring.java ) |O(n)|O(k)|Hard|Two Pointers
13
15
| 56| [ Merge Intervals] ( https://leetcode.com/problems/merge-intervals/ ) | [ Solution] ( ../../blob/master/HARD/src/hard/MergeIntervals.java ) | O(n* logn)| O(1)| Hard|
You can’t perform that action at this time.
0 commit comments