-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution108.java
42 lines (35 loc) · 908 Bytes
/
Solution108.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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 将有序数组转换为二叉搜索树
* @date: 2019/03/07
*/
public class Solution108 {
public TreeNode sortedArrayToBST(int[] nums) {
if (null == nums || 0 >= nums.length) {
return null;
}
return dfs(nums, 0, nums.length - 1);
}
private TreeNode dfs(int[] nums, int start, int end) {
if (start > end) {
return null;
}
if (start == end) {
return new TreeNode(nums[start]);
}
int mid = (start + end) >> 1;
TreeNode node = new TreeNode(nums[mid]);
node.left = dfs(nums, start, mid - 1);
node.right = dfs(nums, mid + 1, end);
return node;
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}