3
3
import com .fishercoder .common .classes .TreeLinkNode ;
4
4
5
5
/**
6
- * /* Follow up for problem "Populating Next Right Pointers in Each Node".
7
-
8
- What if the given tree could be any binary tree? Would your previous solution still work?
6
+ * 117. Populating Next Right Pointers in Each Node II
7
+ *
8
+ * Follow up for problem "Populating Next Right Pointers in Each Node".
9
+ * What if the given tree could be any binary tree? Would your previous solution still work?
9
10
10
11
Note:
11
12
25
26
4-> 5 -> 7 -> NULL */
26
27
27
28
public class _117 {
28
- //copied this post: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
29
- //very clever and concise to make it in O(1) space
29
+ public static class Solution1 {
30
+ //copied this post: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
31
+ //very clever and concise to make it in O(1) space
30
32
31
- //based on level order traversal
32
- public static void connect (TreeLinkNode root ) {
33
+ //based on level order traversal
34
+ public void connect (TreeLinkNode root ) {
33
35
34
- TreeLinkNode head = null ; //head of the next level
35
- TreeLinkNode prev = null ; //the leading node on the next level
36
- TreeLinkNode cur = root ; //current node of current level
36
+ TreeLinkNode head = null ; //head of the next level
37
+ TreeLinkNode prev = null ; //the leading node on the next level
38
+ TreeLinkNode cur = root ; //current node of current level
37
39
38
- while (cur != null ) {
40
+ while (cur != null ) {
39
41
40
- while (cur != null ) { //iterate on the current level
41
- //left child
42
- if (cur .left != null ) {
43
- if (prev != null ) {
44
- prev .next = cur .left ;
45
- } else {
46
- head = cur .left ;
42
+ while (cur != null ) { //iterate on the current level
43
+ //left child
44
+ if (cur .left != null ) {
45
+ if (prev != null ) {
46
+ prev .next = cur .left ;
47
+ } else {
48
+ head = cur .left ;
49
+ }
50
+ prev = cur .left ;
47
51
}
48
- prev = cur . left ;
49
- }
50
- //right child
51
- if ( cur . right != null ) {
52
- if ( prev != null ) {
53
- prev . next = cur .right ;
54
- } else {
55
- head = cur .right ;
52
+ //right child
53
+ if ( cur . right != null ) {
54
+ if ( prev != null ) {
55
+ prev . next = cur . right ;
56
+ } else {
57
+ head = cur .right ;
58
+ }
59
+ prev = cur .right ;
56
60
}
57
- prev = cur .right ;
61
+ //move to next node
62
+ cur = cur .next ;
58
63
}
59
- //move to next node
60
- cur = cur .next ;
61
- }
62
64
63
- //move to next level
64
- cur = head ;
65
- head = null ;
66
- prev = null ;
65
+ //move to next level
66
+ cur = head ;
67
+ head = null ;
68
+ prev = null ;
69
+ }
67
70
}
68
-
69
- }
70
-
71
- public static void main (String ... args ) {
72
- TreeLinkNode root = new TreeLinkNode (1 );
73
- root .left = new TreeLinkNode (2 );
74
- root .right = new TreeLinkNode (3 );
75
- root .left .left = new TreeLinkNode (4 );
76
- root .left .right = new TreeLinkNode (5 );
77
- root .right .right = new TreeLinkNode (7 );
78
- connect (root );
79
71
}
80
- }
72
+ }
0 commit comments