当前位置:网站首页>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);
}
}
边栏推荐
- 鸿蒙第三次培训
- Solution to long waiting time of SSH connection to remote host
- 免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
- 国内如何购买Google Colab会员
- PHP online confusion encryption tutorial sharing + basically no solution
- [combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
- 1147_ Makefile learning_ Target files and dependent files in makefile
- Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
- Simple use of unity pen XR grab
- Take you to API development by hand
猜你喜欢

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

Kubernetes resource object introduction and common commands (III)

How do large consumer enterprises make digital transformation?

Hongmeng third training
![[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

C language modifies files by line

PHP online confusion encryption tutorial sharing + basically no solution

QT learning diary 9 - dialog box

跨境电商:外贸企业做海外社媒营销的优势

Swm32 series Tutorial 4 port mapping and serial port application
随机推荐
[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
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
Type conversion, variable
Leetcode13. Roman numeral to integer (three solutions)
VM11289 WAService. js:2 Do not have __ e handler in component:
C language string inversion
Squid 服务启动脚本
国内如何购买Google Colab会员
基于主机的入侵系统IDS
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
27. Input 3 integers and output them in descending order. Pointer method is required.
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
Rsync remote synchronization
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
LeetCode13.罗马数字转整数(三种解法)
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
Svn full backup svnadmin hotcopy