首页 > 编程语言 >数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)

数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)

时间:2025-01-12 13:59:13浏览次数:3  
标签:right TreeNode nums Ts number 108 二叉树 null left

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

描述

  • 给你一个整数数组 nums ,其中元素已经按 升序 排列
  • 请你将其转换为一棵 平衡 二叉搜索树

示例 1

输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案

示例 2

输入:nums = [1,3]
输出:[3,1]
解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。

提示

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

Typescript 版算法实现


1 ) 方案1: 中序遍历,总是选择中间位置左边的数字作为根节点

/**
 * 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 {
    return helper(nums, 0, nums.length - 1);
}

function helper(nums: number[], left: number, right: number): TreeNode | null {
    if (left > right) return null;
    // Preventing overflow for large arrays by using the following formula
    let mid = left + Math.floor((right - left) / 2);
    let root = new TreeNode(nums[mid]);
    root.left = helper(nums, left, mid - 1);
    root.right = helper(nums, mid + 1, right);
    return root;
}

2 ) 方案2: 中序遍历,总是选择中间位置右边的数字作为根节点

/**
 * 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 {
    return helper(nums, 0, nums.length - 1);
}

function helper(nums: number[], left: number, right: number): TreeNode | null {
    if (left > right) return null;

    // 总是选择中间位置右边的数字作为根节点
    let mid = Math.floor((left + right + 1) / 2); // 加1保证了当长度为偶数时取右中位数
    let root = new TreeNode(nums[mid]);

    root.left = helper(nums, left, mid - 1);
    root.right = helper(nums, mid + 1, right);

    return root;
}

3 ) 方案3: 中序遍历,选择任意一个中间位置数字作为根节点

/**
 * 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 {
    return helper(0, nums.length - 1);

    function helper(left: number, right: number): TreeNode | null {
        if (left > right) return null;

        // 选择任意一个中间位置数字作为根节点
        let mid: number;
        if ((right - left) % 2 === 0) {
            // 如果左右边界之间是奇数个元素,只有一个中间值
            mid = Math.floor((left + right) / 2);
        } else {
            // 如果左右边界之间是偶数个元素,随机选择一个中间值
            mid = left + Math.floor((right - left + Math.random()) / 2);
        }

        const root = new TreeNode(nums[mid]);
        root.left = helper(left, mid - 1);
        root.right = helper(mid + 1, right);
        return root;
    }
}

4 ) 方案4: 简单版本

/**
 * 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) return null
  // 二叉搜索树的中序遍历,就是升序列表
  // 数组中间的位置,可以作为树的根节点
  const mid = Math.floor(nums.length / 2)
  const root = new TreeNode(nums[mid])
  root.left = sortedArrayToBST(nums.slice(0,mid))
  root.right = sortedArrayToBST(nums.slice(mid+1))
  return root
}

标签:right,TreeNode,nums,Ts,number,108,二叉树,null,left
From: https://blog.csdn.net/Tyro_java/article/details/144993168

相关文章