当前位置:网站首页>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);
}
}
边栏推荐
- [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
- SWM32系列教程4-端口映射及串口应用
- Where is the monitoring page of RDS database?
- PHP online confusion encryption tutorial sharing + basically no solution
- How to read the source code [debug and observe the source code]
- 毕业总结
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- 设计电商秒杀
- The largest matrix (H) in a brush 143 monotone stack 84 histogram
- Installation and configuration of network hard disk NFS
猜你喜欢

Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13

鸿蒙第四次培训

互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码

免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据

1164 Good in C

Vs2013 has blocked the installer, and ie10 needs to be installed

One brush 149 force deduction hot question-10 regular expression matching (H)

C language modifies files by line
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
随机推荐
How to read the source code [debug and observe the source code]
Test your trained model
自动渗透测试工具核心功能简述
C language modifies files by line
Talk about several methods of interface optimization
Depth first search of graph
Notes on problems -- watching videos on edge will make the screen green
Squid 服务启动脚本
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
[try to hack] active detection and concealment technology
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
UE4 official charging resources, with a total price of several thousand
Luogu: p2685 [tjoi2012] Bridge
An example of HP array card troubleshooting
What is your income level in the country?
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
Tensorboard quick start (pytoch uses tensorboard)
Apache service suspended asynchronous acceptex failed
RedHat 6.2 configuring ZABBIX
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)