当前位置:网站首页>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;
}
}
边栏推荐
- How spark gets columns in dataframe --column, $, column, apply
- [groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
- Logstash clear sincedb_ Path upload records and retransmit log data
- A preliminary study of geojson
- Browser reflow and redraw
- Ubantu check cudnn and CUDA versions
- Comment faire votre propre robot
- Basic introduction and source code analysis of webrtc threads
- 图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
猜你喜欢
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Recoverable fuse characteristic test
Exciting, 2022 open atom global open source summit registration is hot
Extension and application of timestamp
BiShe - College Student Association Management System Based on SSM
Problems and solutions of converting date into specified string in date class
Beginner redis
看抖音直播Beyond演唱会有感
ubantu 查看cudnn和cuda的版本
随机推荐
logstash清除sincedb_path上传记录,重传日志数据
Reading notes of the beauty of programming
Ffmpeg captures RTSP images for image analysis
Logstash clear sincedb_ Path upload records and retransmit log data
Beginner redis
数据分析思维分析方法和业务知识——分析方法(三)
vSphere实现虚拟机迁移
Leetcode:20220213 week race (less bugs, top 10% 555)
Zhuhai's waste gas treatment scheme was exposed
【文件IO的简单实现】
如何制作自己的机器人
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
云导DNS和知识科普以及课堂笔记
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
An understanding of & array names
Promise
Promise
How spark gets columns in dataframe --column, $, column, apply
The detailed page returns to the list and retains the original position of the scroll bar
View class diagram in idea