Skip to content

Commit f57f860

Browse files
refactor 116
1 parent 143cb6d commit f57f860

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

src/main/java/com/fishercoder/solutions/_116.java

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
/**
66
* 116. Populating Next Right Pointers in Each Node
7-
*
8-
* Given a binary tree
7+
8+
Given a binary tree
99
1010
struct TreeLinkNode {
1111
TreeLinkNode *left;
@@ -27,52 +27,55 @@ You may assume that it is a perfect binary tree (ie, all leaves are at the same
2727
2 3
2828
/ \ / \
2929
4 5 6 7
30+
3031
After calling your function, the tree should look like:
3132
1 -> NULL
3233
/ \
3334
2 -> 3 -> NULL
3435
/ \ / \
35-
4->5->6->7 -> NULL */
36+
4->5->6->7 -> NULL
37+
*/
3638

3739
public class _116 {
38-
public static class Solution1 {
39-
//credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
40-
//based on level order traversal
41-
public void connect(TreeLinkNode root) {
40+
public static class Solution1 {
41+
/**
42+
* credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
43+
* based on level order traversal
44+
*/
45+
public void connect(TreeLinkNode root) {
4246

43-
TreeLinkNode head = null; //head of the next level
44-
TreeLinkNode prev = null; //the leading node on the next level
45-
TreeLinkNode curr = root; //current node of current level
47+
TreeLinkNode head = null; //head of the next level
48+
TreeLinkNode prev = null; //the leading node on the next level
49+
TreeLinkNode curr = root; //current node of current level
4650

47-
while (curr != null) {
48-
while (curr != null) { //iterate on the current level
49-
//left child
50-
if (curr.left != null) {
51-
if (prev != null) {
52-
prev.next = curr.left;
53-
} else {
54-
head = curr.left;
55-
}
56-
prev = curr.left;
57-
}
58-
//right child
59-
if (curr.right != null) {
60-
if (prev != null) {
61-
prev.next = curr.right;
62-
} else {
63-
head = curr.right;
64-
}
65-
prev = curr.right;
66-
}
67-
//move to next node
68-
curr = curr.next;
69-
}
70-
//move to next level
71-
curr = head;
72-
head = null;
73-
prev = null;
51+
while (curr != null) {
52+
while (curr != null) { //iterate on the current level
53+
//left child
54+
if (curr.left != null) {
55+
if (prev != null) {
56+
prev.next = curr.left;
57+
} else {
58+
head = curr.left;
7459
}
60+
prev = curr.left;
61+
}
62+
//right child
63+
if (curr.right != null) {
64+
if (prev != null) {
65+
prev.next = curr.right;
66+
} else {
67+
head = curr.right;
68+
}
69+
prev = curr.right;
70+
}
71+
//move to next node
72+
curr = curr.next;
7573
}
74+
//move to next level
75+
curr = head;
76+
head = null;
77+
prev = null;
78+
}
7679
}
77-
78-
}
80+
}
81+
}

src/test/java/com/fishercoder/_116Test.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
import org.junit.Test;
77

88
public class _116Test {
9-
private static _116.Solution1 solution1;
10-
private static TreeLinkNode root;
9+
private static _116.Solution1 solution1;
10+
private static TreeLinkNode root;
1111

12-
@BeforeClass
13-
public static void setup() {
14-
solution1 = new _116.Solution1();
15-
}
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _116.Solution1();
15+
}
1616

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);
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);
2525

26-
solution1.connect(root);
27-
}
28-
29-
}
26+
solution1.connect(root);
27+
}
28+
}

0 commit comments

Comments
 (0)