Leetcode-Day13

从零开始Leetcode – Day 13

Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
http://blog.csdn.net/linhuanmars/article/details/23904883

前2天得太难了,跳过。以后碰到太难的直接跳过。 明天继续Angular之旅。快要看完了, 加油~~!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @param {number[]} nums
* @return {TreeNode}
*/

var sortedArrayToBST = function(nums) {
if(nums === null || nums.length ===0){
return null;
}
return helper(nums, 0, nums.length -1);
};

var helper = function(nums, l ,r){
if(l > r){
return null;
}
var m = Math.round((l+r)/2);
var root = new TreeNode(nums[m]);
root.left = helper(nums, l, m-1);
root.right = helper(nums, m+1, r);
return root;
}