当前位置:网站首页>108. 将有序数组转换为二叉搜索树(图文并解)
108. 将有序数组转换为二叉搜索树(图文并解)
2022-04-21 18:52:00 【zzu菜】
108. 将有序数组转换为二叉搜索树
给你一个整数数组 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] 都是高度平衡二叉搜索树。
思考
根据有序数组构建搜索二叉树,
因为数组是有序的,所以我们每次不断选取数组(这个数组不等同于上个数组,是切割后的数组)的中间数作为根节点
首先初始化
-
获取数组中间的坐标为rootIndex
-
获取数组中间的值为rootval
-
创建根节点
-
初始化左右边界分别为left,right
进行递归
-
如果left==right代表结束递归
-
如果left≠rootindex ,证明该节点有左孩子,构建其左孩子,并切割数组继续递归
-
如果right≠rootindex ,证明该节点有右孩子,构建其右孩子,并切割数组继续递归
大致不完整流程如图

public static TreeNode sortedArrayToBST(int[] nums) {
if(nums.length==1) return new TreeNode(nums[0]);
int rootindex = nums.length/2;
int rootval = nums[rootindex];
TreeNode root = new TreeNode(rootval);
// left和right代表root子树的边界
// left代表左边界
// right代表右边界
int left = 0;
int right = nums.length-1;
AddNode(root,nums,left,right,rootindex);
return root;
}
private static void AddNode(TreeNode root, int[] nums, int left, int right, int rootIndex) {
// 递归结束条件
if(left==right) return;
// 该节点存在左子树
if(left!=rootIndex){
int rootLeftIndex = (rootIndex-left)/2+left;
TreeNode rootleft= new TreeNode(nums[rootLeftIndex]);
root.left=rootleft;
//接下来对该节点继续构建其左右子树
AddNode(rootleft,nums,left,rootIndex-1,rootLeftIndex);
}
// 该节点存在右子树
if(right!=rootIndex){
int rootRightIndex = (right + 1 - rootIndex)/2 + rootIndex;
TreeNode rootright=new TreeNode(nums[rootRightIndex]);
root.right=rootright;
//接下来对该节点继续构建其左右子树
AddNode(rootright,nums,rootIndex+1,right,rootRightIndex);
}
}
版权声明
本文为[zzu菜]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_40422192/article/details/124325334
边栏推荐
- Chinese NER Using Lattice LSTM 论文解读
- Potential value of zero knowledge proof
- 【js学习笔记四十】复杂工厂模式
- Building local canal middleware for data migration -- Inspiration from cache breakdown
- Excel表格快速生成LaTeX
- PR如何打开MKV文件?MKV文件如何转为mp4,以及MP4如何被imageJ食用?
- What's a good brand of real wireless headphones? High profile flagship Bluetooth headset
- 无线蓝牙耳机哪款比较好用?2022蓝牙耳机推荐
- 原生JS实现FlappyBird游戏 超详细解析
- 并发工具之Semaphore与Exchanger
猜你喜欢

Can we find the next "cheese stick" product in the 2022 Q1 financial report of miaolando?

MySQL数据库学习---第六章多表查询的课后习题

Serialized object + properties + IO framework

Use the replay function of chrome to publish a blog quickly

The Renaissance of digital art, the exploration and rise of Digital Collections

This thing is called a jump watch?

Mysql database learning - Chapter 6 post class exercises of multi table query

ViewPager中Fragment状态保存的哪些事

手把手教会你搭建组件与应用

flutter xcode打包发布失败 Error.90165
随机推荐
Frida hook variable parameters
一文读懂Seek Tiger推出创世节点的意义
This thing is called a jump watch?
【无标题】
Digital IC tutorial 02 quartz prime and Modelsim installation tutorial
Win10 uwp accessing solution files
Use the replay function of chrome to publish a blog quickly
|NO.Z.00063|——————————|BigDataEnd|
Teach you how to build components and applications by hand
【ES6】let、const、解构赋值、模板字符串
毕业三年,一事无成,被迫回老家,一个决定改变一生。
C#多线去对数据库进行添加操作,ManualResetEvent报The number of WaitHandles must be less than or
一文读懂PlatoFarm新经济模型以及生态进展
[games101] assignment 5: simple ray tracing and framework understanding
Can we find the next "cheese stick" product in the 2022 Q1 financial report of miaolando?
Kotlin | 关于 Lazy ,你应该了解的这些事
每日一题系列:汽水瓶
[short-time amplitude spectrum] matlab simulation of short-time amplitude spectrum estimation in speech enhancement
Read the meaning of seek tiger's launch of Genesis node
After summarizing 30 examples, I realized the layout principle of flutter