-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstruct.java
More file actions
47 lines (39 loc) · 1.1 KB
/
Construct.java
File metadata and controls
47 lines (39 loc) · 1.1 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
package BinarySearchTrees;
import java.util.LinkedList;
import java.util.Queue;
/**
* Added Utility Class to create a binary tree to check and test other codes.
*/
public class Construct {
public static Node construct(String[] arr) {
int x = Integer.parseInt(arr[0]);
int n = arr.length;
Node root = new Node(x);
Queue<Node> q = new LinkedList<>();
q.add(root);
int i = 1;
while (i < n - 1) {
Node temp = q.remove();
Node left = new Node(10);
Node right = new Node(10);
if (arr[i].equals("")) {
left = null;
} else {
left.val = Integer.parseInt(arr[i]);
q.add(left);
}
if (arr[i + 1].equals("")) {
right = null;
} else {
right.val = Integer.parseInt(arr[i + 1]);
q.add(right);
}
temp.left = left;
temp.right = right;
i += 2;
}
return root;
}
public static void main(String[] args) {
}
}