-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnecting.java
More file actions
50 lines (38 loc) · 1.2 KB
/
Connecting.java
File metadata and controls
50 lines (38 loc) · 1.2 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Connecting {
public static void connect(TreeNodeI root) {
if (root == null || root.left==null){
return;
}
TreeNodeI start = root;
start.next = null;
TreeNodeI anchor = root;
TreeNodeI first = start.left;
while(anchor.left!=null){
if(start.next==null){
first.next = start.right;
first.next.next = null;
start = anchor.left;
anchor = anchor.left;
first = start.left;
}else{
first.next = start.right;
start = start.next;
first.next.next = start.left;
first=start.left;
}
}
return;
}
public static void main(String args[]){
TreeNodeI root = new TreeNodeI(1);
root.setLeftChild(new TreeNodeI(2));
root.setRightChild(new TreeNodeI(3));
Connecting.connect(root);
System.out.println(root.val);
System.out.println(root.next);
System.out.println(root.left.val);
System.out.println(root.left.next);
System.out.println(root.right.val);
System.out.println(root.right.next);
}
}