We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e5915fe commit db42d21Copy full SHA for db42d21
solution/0117.Populating Next Right Pointers in Each Node II/Solution.java
@@ -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
18
+ preNode = curNode.right;
19
+ first_node_next_layer = curNode.right;
20
21
+ preNode.next = curNode.right;
22
23
24
25
26
+ connect(first_node_next_layer);
27
28
+}
0 commit comments