Skip to content

Commit db42d21

Browse files
117. Populating Next Right Pointers in Each Node II (java)
1 parent e5915fe commit db42d21

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public void connect(TreeLinkNode root) {
3+
if (root == null) return;
4+
TreeLinkNode first_node_next_layer = null;
5+
TreeLinkNode preNode = null;
6+
for (TreeLinkNode curNode = root; curNode != null; curNode = curNode.next) {
7+
if (curNode.left != null) {
8+
if (preNode == null) {
9+
preNode = curNode.left;
10+
first_node_next_layer = curNode.left;
11+
} else {
12+
preNode.next = curNode.left;
13+
preNode = preNode.next;
14+
}
15+
}
16+
if (curNode.right != null) {
17+
if (preNode == null) {
18+
preNode = curNode.right;
19+
first_node_next_layer = curNode.right;
20+
} else {
21+
preNode.next = curNode.right;
22+
preNode = preNode.next;
23+
}
24+
}
25+
}
26+
connect(first_node_next_layer);
27+
}
28+
}

0 commit comments

Comments
 (0)