-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeFromLinkedList.java
43 lines (42 loc) · 1.12 KB
/
BinaryTreeFromLinkedList.java
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Iterator;
class BinaryTreeLinkedList extends Tree{
public Node convertListintoBinaryTree(LinkedList list,Node node){
Iterator it = list.iterator();
Queue<Node> queue = new LinkedList<Node>();
int k=1;
int listsize=list.size();
while(it.hasNext()&&k<listsize){
Node n = new Node((int)it.next());
if(k==1){
node = n;
queue.add(n);
}
else{
Node parentnode = queue.poll();
queue.add(n);
Node rightChild=new Node((int)it.next());
parentnode.left=n;
parentnode.right=rightChild;
queue.add(rightChild);
}
k++;
}
return node;
}
}
public class BinaryTreeFromLinkedList{
public static void main(String args[]){
LinkedList list = new LinkedList();
//using the local tree created by me only
BinaryTreeLinkedList tree = new BinaryTreeLinkedList();
list.add(2);
list.add(3);
list.add(4);
list.add(6);
list.add(5);
Node node = tree.convertListintoBinaryTree(list,tree.root);
tree.display(node);
}
}