Skip to content

Commit 4310a71

Browse files
authored
Merge pull request #3208 from a1exanddrovich/0116-dev
Create: 0116-populating-next-right-pointers-in-each-node.java
2 parents 5aa3027 + 30ed8d7 commit 4310a71

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public Node connect(Node root) {
3+
Node current = root;
4+
Node next = root == null ? null : root.left;
5+
6+
while (current != null && next != null) {
7+
current.left.next = current.right;
8+
9+
if (current.next != null) {
10+
current.right.next = current.next.left;
11+
}
12+
13+
current = current.next;
14+
15+
if (current == null) {
16+
current = next;
17+
next = current.left;
18+
}
19+
}
20+
21+
return root;
22+
}
23+
}

0 commit comments

Comments
 (0)