当前位置:网站首页>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 problem of solving recursive equation with multiple roots | the problem is raised)
- [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
- [combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
- Applet setting multi account debugging
- Vs2013 has blocked the installer, and ie10 needs to be installed
- Squid service startup script
- Open vsftpd port under iptables firewall
- kubernetes资源对象介绍及常用命令(四)
- PR second time
- 在iptables防火墙下开启vsftpd的端口
猜你喜欢

SWM32系列教程4-端口映射及串口应用

鸿蒙第三次培训

Golang单元测试、Mock测试以及基准测试

Notes on problems -- watching videos on edge will make the screen green

Hongmeng third training

Talk about several methods of interface optimization

How do large consumer enterprises make digital transformation?

【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
![[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

Unity notes unityxr simple to use
随机推荐
When absolutely positioned, the element is horizontally and vertically centered
1164 Good in C
WEB-UI自动化测试-最全元素定位方法
[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
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
SSH连接远程主机等待时间过长的解决方法
Play with fancy special effects. This AE super kit is for you
Swm32 series Tutorial 4 port mapping and serial port application
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
Tensorboard quick start (pytoch uses tensorboard)
Web-ui automated testing - the most complete element positioning method
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
AcWing 4489. 最长子序列
27. Input 3 integers and output them in descending order. Pointer method is required.
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
The difference between i++ and ++i: tell their differences easily
Depth first search of graph
New library online | cnopendata complete data of Chinese insurance institution outlets
Kotlin learning quick start (7) -- wonderful use of expansion