-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree5.java
More file actions
53 lines (50 loc) · 1.39 KB
/
Copy pathTree5.java
File metadata and controls
53 lines (50 loc) · 1.39 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
51
52
53
import java.util.ArrayList;
public class Tree5{
static class Loda {
int data;
Loda left;
Loda right;
Loda(int data){
this.data = data;
this.left = null;
this.right= null;
}
}
static class BinaryTree {
int idx = 0;
public Loda buildTree(ArrayList<Integer> array){
idx++;
if(array.get(idx)==-1){
return null;
}
Loda newNode = new Loda(array.get(idx));
newNode.left = buildTree(array);
newNode.right = buildTree(array);
return newNode;
}
}
// public class Hatsh{
// BinaryTree bt = new BinaryTree();
// ArrayList<Integer> alv = new ArrayList<>();
// alv.add();
// Loda rooot = bt.buildTree(alv);
// }
public static void main(String []args){
// int nodes[] = {1,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1};
ArrayList<Integer> al = new ArrayList<>();
// static int vf = 3 ;its wrong we can;t use static
al.add(1);
al.add(2);
al.add(4);
al.add(-1);
al.add(-1);
al.add(5);
al.add(-1);
al.add(-1);
al.add(3);
BinaryTree tree = new BinaryTree();
Loda root = tree.buildTree(al);
System.out.println(root.toString());
// System.out.println(java.util.Arrays.to(root));
}
}