当前位置:网站首页>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
边栏推荐
- Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
- #PAT#day10
- What is the investment value of iFLYTEK, which does not make money?
- 深度解析指针与数组笔试题
- tcpdump: no suitable device found
- RobotFramework入门(一)简要介绍及使用
- Data and Introspection__ dict__ Attributes and__ slots__ attribute
- codeforces每日5題(均1700)-第六天
- Spherical lens and cylindrical lens
- CobaltStrike-4.4-K8修改版安装使用教程
猜你喜欢

Sign SSL certificate as Ca

Era5 reanalysis data download strategy

Performance test method of bank core business system

Eight super classic pointer interview questions (3000 words in detail)

【若依(ruoyi)】启用迷你导航栏

My C language learning record (blue bridge) -- under the pointer
![[ruoyi] set theme style](/img/e9/6a6b7113faed16c3b439230806320b.png)
[ruoyi] set theme style

Taobao focus map layout practice

Fault analysis | analysis of an example of MySQL running out of host memory

微服务注册与发现
随机推荐
Reverse repackaging of wechat applet
2.11 simulation summary
Daily question brushing plan-2-13 fingertip life
Redis cluster deployment based on redis5
[ruoyi] ztree custom icon (iconskin attribute)
[ruoyi] enable Mini navigation bar
4. File modification
Problems encountered in 2022 work IV
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
A copy can also produce flowers
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Linear programming matlab
【若依(ruoyi)】启用迷你导航栏
【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
2.12 simulation
Function knowledge points
RobotFramework入门(一)简要介绍及使用
Codeworks 5 questions per day (1700 average) - day 6
Master data management theory and Practice
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology