当前位置:网站首页>Recursive method converts ordered array into binary search tree
Recursive method converts ordered array into binary search tree
2022-07-06 00:50:00 【Hydrion-Qlz】
108. Convert an ordered array to a binary search tree
The difficulty is simple
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 .
Ideas
The title says that it should be converted into a highly balanced binary search tree . What's the difference between this and converting to an ordinary binary search tree ?
If it is balanced, we will naturally think of dividing the array from the middle of the array , Take the intermediate element as the root node , Use the left node to construct the left subtree , Use the nodes on the right to construct the right subtree , Then recursively construct the elements on the left and right , Until the interval length is 0 until .
This is very similar to dichotomy , Half and half treatment .
Then you need to pay attention to the scope you define , It is suggested to uniformly use the form of left closed right open or both left and right closed in your own coding , Don't switch between the two , It's easy to get dizzy
package cn.edu.xjtu.carlWay.tree.array2BST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 108. Convert an ordered array to a binary search tree * Give you an array of integers nums , Where elements have been Ascending array , Please turn it into a Highly balanced Binary search tree . * <p> * 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 . * <p> * https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/ */
public class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return helpBuild(nums, 0, nums.length);
}
// The range here is left closed and right open , It's especially necessary to understand this
private TreeNode helpBuild(int[] nums, int left, int right) {
if (left == right) {
return null;
}
if (right - left == 1) {
return new TreeNode(nums[left]);
}
// Find the middle root node
int idx = left + (right - left) / 2;
TreeNode node = new TreeNode(nums[idx]);
// Construct the left subtree
node.left = helpBuild(nums, left, idx);
// Construct the right subtree
node.right = helpBuild(nums, idx + 1, right);
return node;
}
}
边栏推荐
- Cloud guide DNS, knowledge popularization and classroom notes
- Extension and application of timestamp
- I'm interested in watching Tiktok live beyond concert
- Getting started with devkit
- Spark AQE
- Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
- Lone brave man
- An understanding of & array names
- curlpost-php
- Kotlin core programming - algebraic data types and pattern matching (3)
猜你喜欢

Leetcode 450 deleting nodes in a binary search tree

KDD 2022 | EEG AI helps diagnose epilepsy

MYSQL GROUP_ The concat function realizes the content merging of the same ID
![[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)](/img/52/021931181ad3f4bef271b4e98105c2.jpg)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)

Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network

Data analysis thinking analysis methods and business knowledge - analysis methods (III)

如何利用Flutter框架开发运行小程序

Analysis of the combination of small program technology advantages and industrial Internet

Ubantu check cudnn and CUDA versions

Browser reflow and redraw
随机推荐
数据分析思维分析方法和业务知识——分析方法(三)
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
An understanding of & array names
Idea远程提交spark任务到yarn集群
KDD 2022 | EEG AI helps diagnose epilepsy
激动人心,2022开放原子全球开源峰会报名火热开启
Hundreds of lines of code to implement a JSON parser
Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
Introduction of motor
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
NLP generation model 2017: Why are those in transformer
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Curlpost PHP
vSphere实现虚拟机迁移
ubantu 查看cudnn和cuda的版本
Leetcode:20220213 week race (less bugs, top 10% 555)
Kotlin core programming - algebraic data types and pattern matching (3)
How to use the flutter framework to develop and run small programs
Idea remotely submits spark tasks to the yarn cluster
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea