当前位置:网站首页>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
边栏推荐
- [ruoyi] set theme style
- My C language learning record (blue bridge) -- under the pointer
- ERA5再分析资料下载攻略
- 电机控制反Park变换和反Clarke变换公式推导
- Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
- 故障分析 | MySQL 耗尽主机内存一例分析
- Game theory matlab
- 建模规范:命名规范
- Eight super classic pointer interview questions (3000 words in detail)
- My C language learning records (blue bridge) -- files and file input and output
猜你喜欢
Maturity of master data management (MDM)
[concept] Web basic concept cognition
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Linear regression and logistic regression
淘宝焦点图布局实战
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
How to accurately identify master data?
Master data management theory and Practice
有没有完全自主的国产化数据库技术
Apt installation ZABBIX
随机推荐
Audio-AudioRecord Binder通信机制
Modeling specifications: naming conventions
Spherical lens and cylindrical lens
Apt installation ZABBIX
Software design principles
主数据管理(MDM)的成熟度
不赚钱的科大讯飞,投资价值该怎么看?
Performance test method of bank core business system
Who is the winner of PTA
Pat 1084 broken keyboard (20 points) string find
Résumé des méthodes de reconnaissance des caractères ocr
原型图设计
jsscript
RobotFramework入门(三)WebUI自动化之百度搜索
Classic interview question [gem pirate]
These are not very good
技术分享 | undo 太大了怎么办
Some problem records of AGP gradle
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用