算法(TS):将有序数组转换为二叉搜索树

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

示例一

输入:nums = [-10,-3,0,5,9]

输出

或者

示例二

输入:nums = [1,3]

输出

提示:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums 按 严格递增 顺序排列

解答一

先创建一个平衡二叉树,再中序遍历它,将数组中的值依次赋值给节点。

scss 复制代码
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function sortedArrayToBST(nums: number[]): TreeNode | null {
    let BST
    // 创建二叉平衡树
    const createBST = (size: number) => {
        const root = new TreeNode()
        size--
        const stack: TreeNode[] = [root]
        while(size > 0 && stack.length) {
            const node = stack.shift()
            const left = new TreeNode()
            node.left = left
            stack.push(left)
            size--
            if (size) {
                const right = new TreeNode()
                node.right = right
                stack.push(right)
            }
            size--
        }

        return root
    }
    // 中序遍历二叉树将数组中的值赋给节点
    const  inorderInsertValue = (root: TreeNode,nums: number[]) => {
        const stack: TreeNode[] = []
        const inorder = (r: TreeNode) => {
            if(r.left) {
                inorder(r.left)
            }
            r.val = nums.shift()
            if (r.right) {
                inorder(r.right)
            }
        }
        inorder(root)
    }

    if (nums.length) {
        BST = createBST(nums.length)
        inorderInsertValue(BST,nums)

    }
    return BST
};

时间复杂度O(n + n),空间复杂度O(height),height为树的高度。

解法二

使用递归创建左右子树的根节点,每一次取数组中间的数作为节点的值。

sql 复制代码
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function sortedArrayToBST(nums: number[]): TreeNode | null {
    if(nums.length === 0) return null

    const createNode = (nums: number[],start: number,end: number):TreeNode | null => {
        if (start > end) return null
        const m = Math.floor((start + end)/2)
        const root = new TreeNode(nums[m])
        root.left = createNode(nums,start,m - 1)
        root.right = createNode(nums,m + 1, end)

        return root
    }

    return createNode(nums,0,nums.length - 1)
};

时间复杂度O(n),空间复杂度O(height),height为树的高度。

相关推荐
NAGNIP2 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
奔跑的蜗牛ing2 小时前
Vue3 + Element Plus 输入框省略号插件:零侵入式全局解决方案
vue.js·typescript·前端工程化
美团技术团队3 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja8 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下8 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶8 小时前
算法 --- 字符串
算法
博笙困了8 小时前
AcWing学习——差分
c++·算法
NAGNIP8 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP8 小时前
大模型微调框架之LLaMA Factory
算法
echoarts8 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust