当前位置:网站首页>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
边栏推荐
- Audio-AudioRecord Binder通信机制
- I sorted out a classic interview question for my job hopping friends
- Daily question brushing plan-2-13 fingertip life
- 微服务间通信
- 1. Dynamic parameters of function: *args, **kwargs
- XSS challenges绕过防护策略进行 XSS 注入
- Inherit day01
- 手写数据库客户端
- Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
- Communication between microservices
猜你喜欢
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
1. Dynamic parameters of function: *args, **kwargs
JS regular filtering and adding image prefixes in rich text
How to accurately identify master data?
OCR文字识别方法综述
Analyze menu analysis
What is the investment value of iFLYTEK, which does not make money?
如何做好功能测试
C language - Blue Bridge Cup - promised score
随机推荐
适合程序员学习的国外网站推荐
OCR文字识别方法综述
PMP每日一练 | 考试不迷路-7.5
Buuctf question brushing notes - [geek challenge 2019] easysql 1
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
Performance test method of bank core business system
如何精准识别主数据?
技术分享 | undo 太大了怎么办
CSP numeric sort
[matlab] access of variables and files
【Unity3D】GUI控件
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
Apt installation ZABBIX
[pointer training - eight questions]
What is the investment value of iFLYTEK, which does not make money?
Self made CA certificate and SSL certificate using OpenSSL
Selenium share
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
Daily question brushing plan-2-13 fingertip life
Mysql database operation