首页 > 其他 > 详细

108. Convert Sorted Array to Binary Search Tree

时间:2019-03-15 16:04:06      阅读:142      评论:0      收藏:0      [点我收藏+]

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

 

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

108. Convert Sorted Array to Binary Search Tree

原文:https://www.cnblogs.com/MarkLeeBYR/p/10537400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!