当前位置:网站首页>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
边栏推荐
- 八道超经典指针面试题(三千字详解)
- Spherical lens and cylindrical lens
- Linear regression and logistic regression
- XSS challenges bypass the protection strategy for XSS injection
- Briefly describe the implementation principle of redis cluster
- Elimination games
- 全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
- Eight super classic pointer interview questions (3000 words in detail)
- 电机控制反Park变换和反Clarke变换公式推导
- Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
猜你喜欢
4. File modification
Codeforces 5 questions par jour (1700 chacune) - jour 6
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
Universal crud interface
[kubernetes series] learn the exposed application of kubernetes service security
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
1. Dynamic parameters of function: *args, **kwargs
八道超经典指针面试题(三千字详解)
Microservice registration and discovery
如何做好功能测试
随机推荐
手写数据库客户端
【概念】Web 基础概念认知
电机控制反Park变换和反Clarke变换公式推导
C # create self host webservice
CSP numeric sort
原型图设计
Inherit day01
ERA5再分析资料下载攻略
My C language learning records (blue bridge) -- files and file input and output
The difference between sizeof and strlen in C language
XSS challenges绕过防护策略进行 XSS 注入
SD card reports an error "error -110 whilst initializing SD card
JS regular filtering and adding image prefixes in rich text
CobaltStrike-4.4-K8修改版安装使用教程
Descriptor implements ORM model
I sorted out a classic interview question for my job hopping friends
微服务间通信
What are the principles of software design (OCP)
Game theory matlab
Spherical lens and cylindrical lens