LeetCode力扣 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree 题解代码 JavaScript

问题 https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/

练习使用JavaScript解答

这个题目的答案并不唯一,因此和测试用例不同也没关系

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
function funBST(nums, beg, end) {
    if(beg > end)
        return null;
    var mid = Math.floor((beg+end)/2);
    var tree = new TreeNode(nums[mid]);
    tree.left = funBST(nums, beg, mid-1);
    tree.right = funBST(nums, mid+1, end);
    return tree;
}
var sortedArrayToBST = function(nums) {
    return funBST(nums, 0, nums.length-1);
};

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页