Skip to content

Commit e52d25e

Browse files
authored
Create Populating Next Right Pointers in Each Node.java
1 parent 723cec8 commit e52d25e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//116. Populating Next Right Pointers in Each Node
2+
class Solution {
3+
public Node connect(Node root) {
4+
Node node = root; // The node just above current needling
5+
6+
while (node != null && node.left != null) {
7+
Node dummy = new Node(); // Dummy node before needling
8+
// Needle children of node
9+
for (Node needle = dummy; node != null; node = node.next) {
10+
needle.next = node.left;
11+
needle = needle.next;
12+
needle.next = node.right;
13+
needle = needle.next;
14+
}
15+
node = dummy.next; // Move node to the next level
16+
}
17+
18+
return root;
19+
}
20+
}

0 commit comments

Comments
 (0)