Skip to content

Latest commit

 

History

History
17 lines (15 loc) · 459 Bytes

108. 将有序数组转换为二叉搜索树.md

File metadata and controls

17 lines (15 loc) · 459 Bytes
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return sortedArrayToBST(nums, 0, nums.length - 1);
    }

    private TreeNode sortedArrayToBST(int[] nums, int l, int r) {
        if (l > r) return null;
        int m = l + r >> 1;
        TreeNode root = new TreeNode(nums[m]);
        root.left = sortedArrayToBST(nums, l, m -  1);
        root.right = sortedArrayToBST(nums, m + 1, r);
        return root;
    }
}