当前位置:网站首页>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
边栏推荐
- Daily question brushing plan-2-13 fingertip life
- Pat 1084 broken keyboard (20 points) string find
- Redis SDS principle
- 解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
- jsscript
- Analyze menu analysis
- [ruoyi] ztree custom icon (iconskin attribute)
- Is there a completely independent localization database technology
- 多态day02
- Rust language -- iterators and closures
猜你喜欢

Problems encountered in 2022 work IV
![[unity3d] GUI control](/img/ef/9de2aa75c67cf825983867a913db74.png)
[unity3d] GUI control

Recommended foreign websites for programmers to learn

1. Dynamic parameters of function: *args, **kwargs

js 正则过滤和增加富文本中图片前缀

Installation and use tutorial of cobaltstrike-4.4-k8 modified version

Who is the winner of PTA

Solution: attributeerror: 'STR' object has no attribute 'decode‘

Game theory matlab

4. File modification
随机推荐
Introduction to robotframework (III) Baidu search of webui automation
Briefly describe the implementation principle of redis cluster
Inherit day01
八道超经典指针面试题(三千字详解)
原型图设计
Data and Introspection__ dict__ Attributes and__ slots__ attribute
Differences and application scenarios between resulttype and resultmap
Linear programming matlab
Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
Derivation of anti Park transform and anti Clarke transform formulas for motor control
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
有没有完全自主的国产化数据库技术
codeforces每日5題(均1700)-第六天
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Performance test method of bank core business system
My C language learning record (blue bridge) -- under the pointer
Who is the winner of PTA
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
C language - Blue Bridge Cup - promised score
How to read excel, PDF and JSON files in R language?