File tree Expand file tree Collapse file tree 1 file changed +14
-12
lines changed
Tree Breadth First Search Expand file tree Collapse file tree 1 file changed +14
-12
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public Node connect (Node root ) {
3
- Node head = root , prev = root ;
3
+ if (root == null ) {
4
+ return null ;
5
+ }
4
6
5
- while (prev != null ) {
6
- Node curr = prev ;
7
+ Node leftMostNode = root ;
7
8
8
- while (curr != null ) {
9
- if (curr .left != null ) {
10
- curr .left .next = curr .right ;
11
- }
9
+ while (leftMostNode .left != null ) {
10
+ Node head = leftMostNode ;
11
+
12
+ while (head != null ) {
13
+ head .left .next = head .right ;
12
14
13
- if (curr . right != null && curr .next != null ) {
14
- curr .right .next = curr .next .left ;
15
+ if (head .next != null ) {
16
+ head .right .next = head .next .left ;
15
17
}
16
18
17
- curr = curr .next ;
19
+ head = head .next ;
18
20
}
19
21
20
- prev = prev .left ;
22
+ leftMostNode = leftMostNode .left ;
21
23
}
22
24
23
- return head ;
25
+ return root ;
24
26
}
25
27
}
You can’t perform that action at this time.
0 commit comments