-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117.java
More file actions
27 lines (27 loc) · 939 Bytes
/
117.java
File metadata and controls
27 lines (27 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
if (root.left != null) {
TreeLinkNode next = root.right;
TreeLinkNode rootnext = root;
while (next == null && rootnext.next != null){
rootnext = rootnext.next;
if (rootnext.left == null) next = rootnext.right;
else next = rootnext.left;
}
root.left.next = next;
}
if (root.right != null) {
TreeLinkNode next = null;
TreeLinkNode rootnext = root.next;
while (next == null && rootnext != null){
if (rootnext.left == null) next = rootnext.right;
else next = rootnext.left;
rootnext = rootnext.next;
}
root.right.next = next;
}
connect(root.right);
connect(root.left);
}
}