当前位置:网站首页>Recursive construction of maximum binary tree
Recursive construction of maximum binary tree
2022-07-07 08:04:00 【Hydrion-Qlz】
654. The largest binary tree
Medium difficulty
Given an array of non repeating integers nums
. The largest binary tree You can use the following algorithm from nums
Build... Recursively :
- Create a root node , Its value is
nums
Maximum of . - Recursively at maximum On the left Of Subarray prefix Build the left subtree .
- Recursively at maximum On the right Of Subarray suffix Build the right subtree .
return nums
Built The largest binary tree .
Ideas
This problem can be solved by recursion , Similar to the solution of fast platoon
We find the maximum in the current range , Then recurse its left and right ranges constantly , Repeatedly find the maximum value of the current range and recurse , Until the length of the interval is 0
package cn.edu.xjtu.carlWay.tree.maximumBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 654. The largest binary tree * Given an array of non repeating integers nums . The largest binary tree You can use the following algorithm from nums Build... Recursively : * <p> * Create a root node , Its value is nums Maximum of . * Recursively at maximum On the left Of Subarray prefix Build the left subtree . * Recursively at maximum On the right Of Subarray suffix Build the right subtree . * return nums Built The largest binary tree . * <p> * https://leetcode-cn.com/problems/maximum-binary-tree/ */
public class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return helpConstruct(nums, 0, nums.length);
}
private TreeNode helpConstruct(int[] nums, int left, int right) {
// No element in interval
if (left == right) {
return null;
}
// There is one element left in the interval
if (right - left == 1) {
return new TreeNode(nums[left]);
}
int maxIdx = findMaxIdx(nums, left, right);// Find the maximum value of the current interval
TreeNode root = new TreeNode(nums[maxIdx]);
root.left = helpConstruct(nums, left, maxIdx);// Recursive processing
root.right = helpConstruct(nums, maxIdx + 1, right);// Recursive processing
return root;
}
/** * Find the maximum value of the current interval * * @param nums * @param left * @param right * @return */
private int findMaxIdx(int[] nums, int left, int right) {
int max = nums[left];
int maxIdx = left;
for (int i = left; i < right; i++) {
if (nums[i] > max) {
max = nums[i];
maxIdx = i;
}
}
return maxIdx;
}
}
边栏推荐
- 2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
- Wechat applet data binding multiple data
- JS quick start (I)
- dash plotly
- Linux server development, MySQL index principle and optimization
- [2022 actf] Web Topic recurrence
- 探索Cassandra的去中心化分布式架构
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- 3D reconstruction - stereo correction
- Leanote private cloud note building
猜你喜欢
JSON data flattening pd json_ normalize
快速使用 Jacoco 代码覆盖率统计
JS quick start (I)
Quickly use Jacobo code coverage statistics
Introduction to basic components of wechat applet
Custom class loader loads network class
QT learning 26 integrated example of layout management
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
解决问题:Unable to connect to Redis
Common method signatures and meanings of Iterable, collection and list
随机推荐
这5个摸鱼神器太火了!程序员:知道了快删!
Linux server development, MySQL process control statement
Leetcode 90: subset II
MySQL multi column index (composite index) features and usage scenarios
Button wizard script learning - about tmall grabbing red envelopes
mysql多列索引(组合索引)特点和使用场景
Redis technology leak detection and filling (II) - expired deletion strategy
青龙面板-今日头条
Cnopendata geographical distribution data of religious places in China
Shell 脚本的替换功能实现
Thinkcmf6.0 installation tutorial
Linux server development, MySQL cache strategy
Implementation of replacement function of shell script
C语言队列
Main window in QT learning 27 application
Rust Versus Go(哪种是我的首选语言?)
Explore dry goods! Apifox construction ideas
Operation suggestions for today's spot Silver
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Téléchargement des données de conception des puces