当前位置:网站首页>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
边栏推荐
- Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
- Solution: attributeerror: 'STR' object has no attribute 'decode‘
- BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
- RobotFramework入门(一)简要介绍及使用
- jsscript
- 【paddle】加载模型权重后预测报错AttributeError: ‘Model‘ object has no attribute ‘_place‘
- Microservice registration and discovery
- 深度解析指针与数组笔试题
- Differences and application scenarios between resulttype and resultmap
- Day 50 - install vsftpd on ceontos6.8
猜你喜欢

My C language learning records (blue bridge) -- files and file input and output

全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)

OCR文字識別方法綜述

Linear regression and logistic regression
![BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1](/img/37/c38a933ce7fa5d2b8fa597965ffcb2.png)
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1

Microservice registration and discovery

Selenium share
![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)
[concept] Web basic concept cognition

Maturity of master data management (MDM)

A copy can also produce flowers
随机推荐
Mysql database operation
Derivation of anti Park transform and anti Clarke transform formulas for motor control
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
codeforces每日5题(均1700)-第六天
Some problem records of AGP gradle
jsscript
NR modulation 1
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
How to do function test well
Inherit day01
Daily question brushing plan-2-13 fingertip life
Codeworks 5 questions per day (1700 average) - day 6
如何精准识别主数据?
A copy can also produce flowers
ERA5再分析资料下载攻略
[kubernetes series] learn the exposed application of kubernetes service security
07 singleton mode
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
Web security SQL injection vulnerability (1)