-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathConvertSortedArrayToBinarySearchTree.java
67 lines (51 loc) · 1.33 KB
/
ConvertSortedArrayToBinarySearchTree.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package problems.leetcode;
/**
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
*/
public class ConvertSortedArrayToBinarySearchTree {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
@Override
public String toString() {
return "TreeNode{" +
"val=" + val +
'}';
}
}
public static TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length < 1) {
return null;
}
return convert(nums, 0, nums.length - 1);
}
private static TreeNode convert(int[] nums, int l, int r) {
if (l > r) {
return null;
}
int m = (l + r) / 2;
TreeNode root = new TreeNode(nums[m]);
root.left = convert(nums, l, m - 1);
root.right = convert(nums, m + 1, r);
return root;
}
public static void main(String[] args) {
int[] nums = {0, 1, 2};
TreeNode root = sortedArrayToBST(nums);
System.out.println(root);
}
/*
0,1,2,3,4,5,6,7,8,9
4
/ \
1 7
/ \ / \
0 3 6 9
/ / /
2 5 8
*/
}