当前位置:网站首页>Leetcode 108 converts an ordered array into a binary search tree -- recursive method
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
2022-07-03 17:27:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
The question :
Give you an array of integers nums , Where elements have been Ascending array , Please turn it into a Highly balanced Binary search tree .
Highly balanced A binary tree is a tree that satisfies 「 The absolute value of the height difference between the left and right subtrees of each node does not exceed 1 」 Two fork tree .
Example 1:
Input :nums = [-10,-3,0,5,9]
Output :[0,-3,9,-10,null,5]
explain :[0,-10,5,null,-3,null,9] Will also be considered the right answer :
Example 2:
Input :nums = [1,3]
Output :[3,1]
explain :[1,3] and [3,1] They're all highly balanced binary search trees .
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums Press Strictly increasing Sequential arrangement
Ideas :
I'll finish this problem recursively .
In recursion , If it involves the deletion or addition of binary tree nodes , Then the way of returning nodes using recursive methods can make it more convenient and the code more concise . Because this problem involves the construction of binary tree , That is, the increase of nodes , Therefore, we also use recursive methods to return nodes .
Because the input data given by the topic has been arranged in order , In that case , You're welcome , Directly select the middle value in the current array in each recursive method , The tree constructed in this way is naturally a specific tree in line with high balance . For the cutting of arrays , Not every time new The operation of an array , But use Each time a recursive method is called, the left and right subscripts are passed in The way to achieve .
Note that in this question is used Left and right closed The range of , That is, the nodes of the left and right subscripts of the range are reachable .
What's more, we need to pay attention to , When I first took the position of the middle element of the array, I wrote int mid = (left + right) >> 1;
Although this problem can pass , But this way of writing , If left and right Values are greater than int Half the maximum , Operation will Transboundary ,mid What you get is a negative number ! So it should be written like this :int mid = left + ((right - left) / 2);
This topic Java Code :
class Solution {
private TreeNode construct(int[] nums, int left, int right) {
if (left > right) return null;
int mid = left + ((right - left) / 2);
TreeNode node = new TreeNode(nums[mid]);
node.left = construct(nums, left, mid - 1);
node.right = construct(nums, mid + 1, right);
return node;
}
public TreeNode sortedArrayToBST(int[] nums) {
return construct(nums, 0, nums.length - 1);
}
}
边栏推荐
- Where is the monitoring page of RDS database?
- Host based intrusion system IDS
- Enterprise custom form engine solution (XI) -- form rule engine 1
- vs code 插件 koroFileHeader
- IntelliJ 2021.3 short command line when running applications
- AcWing 4489. Longest subsequence
- Where is the database account used when running SQL tasks in data warehouse tasks configured
- 国内如何购买Google Colab会员
- QT adjust win screen brightness and sound size
- 简单配置PostFix服务器
猜你喜欢
IntelliJ 2021.3 short command line when running applications
vs2013已阻止安装程序,需安装IE10
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
新库上线 | CnOpenData中国保险机构网点全集数据
How to train mask r-cnn model with your own data
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
鸿蒙第三次培训
Wechat applet for the first time
大消费企业怎样做数字化转型?
Kubernetes resource object introduction and common commands (III)
随机推荐
RedHat 6.2 配置 Zabbix
人生还在迷茫?也许这些订阅号里有你需要的答案!
1146_ SiCp learning notes_ exponentiation
Wechat applet for the first time
How do large consumer enterprises make digital transformation?
vs2013已阻止安装程序,需安装IE10
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
New library online | cnopendata complete data of Chinese insurance institution outlets
[UE4] brush Arctic pack high quality Arctic terrain pack
Squid service startup script
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
Great changes! National housing prices fell below the 10000 yuan mark
一位普通程序员一天工作清单
First day of rhcsa study
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
鸿蒙第三次培训
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
C language string inversion
Simple use of unity pen XR grab