当前位置:网站首页>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;
}
}
边栏推荐
- LeetCode 90:子集 II
- Linux server development, redis protocol and asynchronous mode
- Cnopendata American Golden Globe Award winning data
- Li Kou interview question 04.01 Path between nodes
- Info | webrtc M97 update
- Ansible
- Visualization Document Feb 12 16:42
- JS quick start (I)
- Force buckle 145 Binary Tree Postorder Traversal
- 2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
猜你喜欢
自定义类加载器加载网络Class
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
Linux server development, MySQL index principle and optimization
即刻报名|飞桨黑客马拉松第三期等你挑战
Common method signatures and meanings of Iterable, collection and list
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
2022焊工(初级)判断题及在线模拟考试
探索干货篇!Apifox 建设思路
Linux server development, redis protocol and asynchronous mode
php导出百万数据
随机推荐
Hands on deep learning (IV) -- convolutional neural network CNN
B. Value sequence thinking
LeetCode 90:子集 II
[guess-ctf2019] fake compressed packets
Detailed explanation of Kalman filter for motion state estimation
Problem solving: unable to connect to redis
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
Pytest+allure+jenkins environment -- completion of pit filling
Linux Installation MySQL 8.0 configuration
Rust versus go (which is my preferred language?)
C language queue
Figure out the working principle of gpt3
Chip design data download
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
[mathematical notes] radian
Button wizard script learning - about tmall grabbing red envelopes
Linux server development, MySQL stored procedures, functions and triggers
Use and analysis of dot function in numpy
Leetcode 40: combined sum II
JS quick start (I)