当前位置:网站首页>Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
2022-07-06 03:07:00 【Snowy solitary boat】
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums,0,nums.length);
}
private TreeNode sortedArrayToBST(int[] nums,int low,int high){
if (low>=high) return null;
int middle = (low+high)>>1;
TreeNode root = new TreeNode(nums[middle]);
root.left = sortedArrayToBST(nums,low,middle);
root.right = sortedArrayToBST(nums,middle+1,high);
return root;
}
Ideas : Since we need to balance , So clearly , The middle node is balanced as the root node
Then further divide the intervals of the left and right subtrees , Recursive left and right subtrees , You can complete the topic
边栏推荐
- Fault analysis | analysis of an example of MySQL running out of host memory
- SD card reports an error "error -110 whilst initializing SD card
- Reverse repackaging of wechat applet
- Rust language -- iterators and closures
- MySQL advanced notes
- These are not very good
- [ruoyi] enable Mini navigation bar
- The difference between sizeof and strlen in C language
- 【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
- 【指针训练——八道题】
猜你喜欢

不赚钱的科大讯飞,投资价值该怎么看?
![[ruoyi] enable Mini navigation bar](/img/28/a8b38aecd90c8ddc98333f0e2d3eab.png)
[ruoyi] enable Mini navigation bar

Is there a completely independent localization database technology

NR modulation 1

Modeling specifications: naming conventions

MySQL advanced notes

C language - Blue Bridge Cup - promised score

Linear programming matlab

解决:AttributeError: ‘str‘ object has no attribute ‘decode‘

主数据管理(MDM)的成熟度
随机推荐
The difference between sizeof and strlen in C language
Codeforces 5 questions par jour (1700 chacune) - jour 6
Era5 reanalysis data download strategy
1. Dynamic parameters of function: *args, **kwargs
jsscript
Prototype design
ERA5再分析资料下载攻略
RobotFramework入门(一)简要介绍及使用
Game theory matlab
Derivation of anti Park transform and anti Clarke transform formulas for motor control
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
SD卡報錯“error -110 whilst initialising SD card
A copy can also produce flowers
CobaltStrike-4.4-K8修改版安装使用教程
Daily question brushing plan-2-13 fingertip life
Atcoder beginer contest 233 (a~d) solution
PMP practice once a day | don't get lost in the exam -7.5
RobotFramework入门(二)appUI自动化之app启动
Reverse repackaging of wechat applet
Sign SSL certificate as Ca