forked from IDeserve/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArrayToBalancedBST
More file actions
89 lines (67 loc) · 1.75 KB
/
SortedArrayToBalancedBST
File metadata and controls
89 lines (67 loc) · 1.75 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package bst;
/**
* <b>IDeserve <br>
* <a href="https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve</a>
* <br><br>
* Create a balanced Binary Search Tree (BST) from a sorted array</b><br>
* Given a sorted integer array of length n, create a balanced Binary Search Tree using the values of the array. <br><br>
* <br><br>
* <a href="https://www.youtube.com/watch?v=VCTP81Ij-EM">Sorted array to balanced bst - Youtube Link</a>
* @author Saurabh
*
*/
public class SortedArrayToBalancedBST {
public static void main(String[] args) {
int array[] = { 3, 6, 8, 23, 48, 76, 89 };
TreeNode treeRoot = createBST(array);
inorder(treeRoot);
}
public static TreeNode createBST(int array[]) {
return createBST(array, 0, array.length-1);
}
private static TreeNode createBST(int[] array, int start, int end) {
if(array == null || array.length == 0 || start > end) {
return null;
}
int mid = (start + end)/2;
TreeNode root = new TreeNode(array[mid]);
root.setLeft(createBST(array, start, mid-1));
root.setRight(createBST(array, mid+1, end));
return root;
}
public static void inorder(TreeNode root) {
if(root == null) {
return;
}
inorder(root.getLeft());
System.out.print(root.getData() + " ");
inorder(root.getRight());
}
}
class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode(int data) {
super();
this.data = data;
}
}