Skip to content

Commit bdcdaff

Browse files
committed
populatingNextRightPointersInEachNode
1 parent b10891f commit bdcdaff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* public class TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode left, right, next;
6+
* TreeLinkNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public void connect(TreeLinkNode root) {
11+
if (root == null) return;
12+
root.next = null;
13+
while (root != null) {
14+
TreeLinkNode current = root;
15+
while (current != null) {
16+
TreeLinkNode a = current.left;
17+
TreeLinkNode b = current.right;
18+
if (a == null) break;
19+
a.next = b;
20+
if (current.next != null) {
21+
b.next = current.next.left;
22+
} else {
23+
b.next = null;
24+
}
25+
current = current.next;
26+
}
27+
root = root.left;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)