We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 56a4576 commit 6b125d1Copy full SHA for 6b125d1
Tree Breadth First Search/116_Populating_Next_Right_Pointers_in_Each_Node.java
@@ -4,22 +4,26 @@ public Node connect(Node root) {
4
return null;
5
}
6
7
- Node leftMostNode = root;
+ Node curr = root;
8
9
- while (leftMostNode.left != null) {
10
- Node head = leftMostNode;
+ while (curr != null) {
+ Node next = curr.left;
11
12
- while (head != null) {
13
- head.left.next = head.right;
+ if (next == null) {
+ break;
14
+ }
15
+
16
17
+ curr.left.next = curr.right;
18
- if (head.next != null) {
- head.right.next = head.next.left;
19
+ if (curr.next != null) {
20
+ curr.right.next = curr.next.left;
21
22
- head = head.next;
23
+ curr = curr.next;
24
25
- leftMostNode = leftMostNode.left;
26
+ curr = next;
27
28
29
return root;
0 commit comments