当前位置:网站首页>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 (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- Hongmeng fourth training
- How SVN views modified file records
- SVN如何查看修改的文件记录
- 国内如何购买Google Colab会员
- 1164 Good in C
- Unity notes unityxr simple to use
- Qt调节Win屏幕亮度和声音大小
- Detailed explanation of common network attacks
猜你喜欢

SQL injection database operation foundation

IntelliJ 2021.3 short command line when running applications

One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
![Luogu: p2685 [tjoi2012] Bridge](/img/f5/f77027288a211ae466781b09ce650f.jpg)
Luogu: p2685 [tjoi2012] Bridge

Type conversion, variable

【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use

POM in idea XML graying solution
![[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th](/img/f1/c96c4a6d34e1ae971f492f4aed5a93.jpg)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th

One brush 145 force deduction hot question-2 sum of two numbers (m)
随机推荐
The largest matrix (H) in a brush 143 monotone stack 84 histogram
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
Apache service suspended asynchronous acceptex failed
Simple use of unity pen XR grab
AcWing 3438. 数制转换
Open vsftpd port under iptables firewall
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
AcWing 3438. Number system conversion
毕业总结
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
New library online | cnopendata complete data of Chinese insurance institution outlets
Thread pool: the most common and error prone component of business code
Vs code plug-in korofileheader
Wechat applet for the first time
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
AcWing 4489. Longest subsequence
New library online | cnopendata China bird watching record data
How to read the source code [debug and observe the source code]