欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)

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

2025/1/8 8:21:02 来源:https://blog.csdn.net/Tyro_java/article/details/144993168  浏览:    关键词:数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)

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

  • https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/

描述

  • 给你一个整数数组 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 formulalet 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
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com