-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ108.java
More file actions
40 lines (40 loc) · 1.1 KB
/
Q108.java
File metadata and controls
40 lines (40 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
/*
* @Author: Shawn Yang
* @Date: 2019-09-12 01:17:51
* @Last Modified by: Shawn Yang
* @Last Modified time: 2019-09-12 01:18:03
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return helperBST(nums, 0, nums.length - 1);
}
public TreeNode helperBST(int[] nums, int start, int end) {
if(start == end) {
return new TreeNode(nums[start]);
}
else if(end - start == 1) {
TreeNode current = new TreeNode(nums[end]);
current.left = new TreeNode(nums[start]);
return current;
}
else if(end < start) {
return null;
}
else {
int middle = (end + start) / 2;
TreeNode current = new TreeNode(nums[middle]);
current.left = helperBST(nums, start, middle - 1);
current.right = helperBST(nums, middle + 1, end);
return current;
}
}
}