Skip to content

Commit 6b125d1

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 116_Populating_Next_Right_Pointers_in_Each_Node.java
1 parent 56a4576 commit 6b125d1

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Tree Breadth First Search/116_Populating_Next_Right_Pointers_in_Each_Node.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ public Node connect(Node root) {
44
return null;
55
}
66

7-
Node leftMostNode = root;
7+
Node curr = root;
88

9-
while (leftMostNode.left != null) {
10-
Node head = leftMostNode;
9+
while (curr != null) {
10+
Node next = curr.left;
1111

12-
while (head != null) {
13-
head.left.next = head.right;
12+
if (next == null) {
13+
break;
14+
}
15+
16+
while (curr != null) {
17+
curr.left.next = curr.right;
1418

15-
if (head.next != null) {
16-
head.right.next = head.next.left;
19+
if (curr.next != null) {
20+
curr.right.next = curr.next.left;
1721
}
1822

19-
head = head.next;
23+
curr = curr.next;
2024
}
2125

22-
leftMostNode = leftMostNode.left;
26+
curr = next;
2327
}
2428

2529
return root;

0 commit comments

Comments
 (0)