当前位置:网站首页>108. Convert an ordered array into a binary search tree (picture and text merging)
108. Convert an ordered array into a binary search tree (picture and text merging)
2022-04-21 19:10:00 【Zzu dish】
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 .
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,null,3] and [3,1] They're all highly balanced binary search trees .
reflection
Build a search Binary Tree Based on an ordered array ,
Because arrays are ordered , So we keep selecting arrays every time ( This array is not the same as the previous array , Is the array after cutting ) Take the middle number of as the root node
Initialize first
-
Get the coordinates in the middle of the array as rootIndex
-
Get the value in the middle of the array as rootval
-
Create a root node
-
Initialize the left and right boundaries as left,right
Recursion
-
If left==right Represents the end of recursion
-
If left≠rootindex , Prove that the node has a left child , Build its left child , And cut the array to continue recursion
-
If right≠rootindex , Prove that the node has a right child , Build its right child , And cut the array to continue recursion
The roughly incomplete process is shown in the figure

public static TreeNode sortedArrayToBST(int[] nums) {
if(nums.length==1) return new TreeNode(nums[0]);
int rootindex = nums.length/2;
int rootval = nums[rootindex];
TreeNode root = new TreeNode(rootval);
// left and right representative root Boundary of subtree
// left Represents the left border
// right Represents the right border
int left = 0;
int right = nums.length-1;
AddNode(root,nums,left,right,rootindex);
return root;
}
private static void AddNode(TreeNode root, int[] nums, int left, int right, int rootIndex) {
// Recursion end condition
if(left==right) return;
// The node has a left subtree
if(left!=rootIndex){
int rootLeftIndex = (rootIndex-left)/2+left;
TreeNode rootleft= new TreeNode(nums[rootLeftIndex]);
root.left=rootleft;
// Next, continue to build its left and right subtrees for this node
AddNode(rootleft,nums,left,rootIndex-1,rootLeftIndex);
}
// The node has a right subtree
if(right!=rootIndex){
int rootRightIndex = (right + 1 - rootIndex)/2 + rootIndex;
TreeNode rootright=new TreeNode(nums[rootRightIndex]);
root.right=rootright;
// Next, continue to build its left and right subtrees for this node
AddNode(rootright,nums,rootIndex+1,right,rootRightIndex);
}
}
版权声明
本文为[Zzu dish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211852089396.html
边栏推荐
- XD signal and system
- uniapp 运行到内置浏览器,怎么打开开发者工具
- How to open the developer tool when the uniapp runs to the built-in browser
- Distributed transaction Foundation
- 5.1 overview of triggers in fundamentals of digital electronic technology
- 2021年拍卖行业发展研究报告
- MusicPlayer2.1版本
- AlwaysInstallElevated提权
- mysql日期转星期
- APM industry awareness series - IX
猜你喜欢

全职加入清华,丘成桐:为祖国、为全球数学界培养数学人才

In the El input search in ement, the matching input suggestions after input must have the value attribute

Edgeboard records

Analysis of ribbon principle and Nacos service discovery principle

2022.04.21 (lc_435_no overlapping interval)

排序会了递归,不学非递归太可惜了

【C语言进阶】⑥函数指针详解

【网络协议】ip addr

2022.04.21(LC_452_用最少数量的箭引爆气球)

自动控制原理第5章——频率法(思维导图)
随机推荐
2022.04.21(LC_435_无重叠区间)
leetcode:423. Reconstructing numbers from English
Introduction to GPS Beidou satellite time synchronization system (clock device) technology of power grid
Database advanced learning: storage engine
【C语言进阶】⑥函数指针详解
全职加入清华,丘成桐:为祖国、为全球数学界培养数学人才
[deep eye] emotion analysis -- Text Classification textrnn based on cyclic neural network for multi task learning
mysql (三) 索引优化以及案例分析
Apply El tooltip (bubble text prompt box) in El tabs
SSH keygen setting password free login
Context in programming
初等数学建模问题
31 - GRU原理与源码逐行实现
APM industry awareness series - III
To deal with doget & dopost Chinese garbled code
Feign source code analysis
APM industry awareness series - VII - 17 Ways to define Devops
Navicat MySQL连接Linux下MySQL的及2003错误解决方案
APM industry awareness series - II
APM 行业认知系列 - 四