108. Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
解题思路:
用preorder traversal来解决

代码:
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums == null || nums.length == 0) return null;
return helper(nums, 0, nums.length - 1);
}

public TreeNode helper(int[] nums, int low, int high){
    if(low > high) return null;
    int mid = (high - low) / 2 + low;
    TreeNode node = new TreeNode(nums[mid]);
    node.left = helper(nums, low, mid - 1);
    node.right = helper(nums, mid + 1, high);
    return node;
}

}

你可能感兴趣的:(108. Convert Sorted Array to Binary Search Tree)