当前位置:网站首页>LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
2022-06-26 05:13:00 【一瓢江湖我沉浮】
1.题目
给你一个整数数组 nums ,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。
高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
示例 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 <= 104
-104 <= nums[i] <= 104
nums 按严格递增顺序排列
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree
2.思路
(1)递归构造
二叉树的构造过程大致为先:构造根节点,然后递归构建左右子树。而一个有序数组对于二叉搜索树来说就是其中序遍历结果,根节点在数组中心,数组左侧是左子树元素,右侧是右子树元素。
3.代码实现(Java)
//思路1————递归构造
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return build(nums, 0, nums.length - 1);
}
//将 nums[left...right] 转换为一棵高度平衡二叉搜索树
public TreeNode build(int[] nums, int left, int right) {
if (left > right) {
//nums[left...right]为空
return null;
}
//构造根节点
int mid = (left + right) / 2;
TreeNode root = new TreeNode(nums[mid]);
//递归构造左子树
root.left = build(nums, left, mid - 1);
//递归构造右子树
root.right = build(nums, mid + 1, right);
return root;
}
}
边栏推荐
- FastAdmin Apache下设置伪静态
- Official image acceleration
- [ide (imagebed)]picgo+typora+aliyunoss deployment blog Gallery (2022.6)
- 【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
- Apktool tool usage document
- 6.1 - 6.2 公鑰密碼學簡介
- 超高精度定位系统中的UWB是什么
- Protocol selection of mobile IM system: UDP or TCP?
- 递归遍历目录结构和树状展现
- [geek] product manager training camp
猜你喜欢

Second day of deep learning and tensorfow

Create SSH key pair configuration steps

Ad tutorial series | 4 - creating an integration library file

6.1 - 6.2 公钥密码学简介

Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing

Apktool tool usage document
Technical problems to be faced in mobile terminal im development

Experience of reading the road to wealth and freedom

6.1 - 6.2 Introduction à la cryptographie à clé publique

Zuul 实现动态路由
随机推荐
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
RESNET practice in tensorflow
微服务之间的Token传递之一@Feign的token传递
cartographer_optimization_problem_2d
Vie procédurale
5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
[unity3d] collider assembly
C# 40. Byte[] to hexadecimal string
创建 SSH 秘钥对 配置步骤
Datetime data type - min() get the earliest date and date_ Range() creates a date range, timestamp() creates a timestamp, and tz() changes the time zone
zencart新建的URL怎么重写伪静态
Codeforces Round #802 (Div. 2)(A-D)
二次bootloader关于boot28.asm应用的注意事项,28035的
Happy New Year!
Schematic diagram of UWB ultra high precision positioning system
Difference between return and yield
Technical problems to be faced in mobile terminal im development
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
[ide (imagebed)]picgo+typora+aliyunoss deployment blog Gallery (2022.6)
Protocol selection of mobile IM system: UDP or TCP?