-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_108.java
More file actions
40 lines (33 loc) · 840 Bytes
/
Solution_108.java
File metadata and controls
40 lines (33 loc) · 840 Bytes
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
package com.hilbert25.leetcode;
/**
* @author : hilbert25
* @version 创建时间:2017年5月9日 下午3:57:45 LeetCode com.hilbert25.leetcode
* Solution_108
*/
public class Solution_108 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode sortedArrayToBST(int[] nums) {
if (nums.length == 0)
return null;
return createNode(0, nums.length - 1, nums);
}
public TreeNode createNode(int begin, int end, int[] nums) {
if (begin > end)
return null;
int middle = (begin + end) / 2;
TreeNode node = new TreeNode(nums[middle]);
node.left = createNode(begin, middle - 1, nums);
node.right = createNode(middle + 1, end, nums);
return node;
}
}