Skip to content

Commit 37620b9

Browse files
committedNov 18, 2017
[N-0] refactor 117
1 parent 5eafc82 commit 37620b9

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed
 

‎src/main/java/com/fishercoder/solutions/_117.java

+38-46
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import com.fishercoder.common.classes.TreeLinkNode;
44

55
/**
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?
910
1011
Note:
1112
@@ -25,56 +26,47 @@
2526
4-> 5 -> 7 -> NULL */
2627

2728
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
3032

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) {
3335

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
3739

38-
while (cur != null) {
40+
while (cur != null) {
3941

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;
4751
}
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;
5660
}
57-
prev = cur.right;
61+
//move to next node
62+
cur = cur.next;
5863
}
59-
//move to next node
60-
cur = cur.next;
61-
}
6264

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+
}
6770
}
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);
7971
}
80-
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeLinkNode;
4+
import com.fishercoder.solutions._117;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _117Test {
9+
private static _117.Solution1 solution1;
10+
private static TreeLinkNode root;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _117.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
root = new TreeLinkNode(1);
20+
root.left = new TreeLinkNode(2);
21+
root.right = new TreeLinkNode(3);
22+
root.left.left = new TreeLinkNode(4);
23+
root.left.right = new TreeLinkNode(5);
24+
root.right.right = new TreeLinkNode(7);
25+
26+
solution1.connect(root);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.