当前位置:网站首页>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
numsMaximum 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;
}
}
边栏推荐
- 有 Docker 谁还在自己本地安装 Mysql ?
- QT learning 28 toolbar in the main window
- Zhilian + AV, AITO asked M7 to do more than ideal one
- Codeforce c.strange test and acwing
- 探索干货篇!Apifox 建设思路
- 2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
- Lattice coloring - matrix fast power optimized shape pressure DP
- 贝叶斯定律
- 芯片 設計資料下載
- PHP exports millions of data
猜你喜欢

Linux server development, redis protocol and asynchronous mode

padavan手动安装php

这5个摸鱼神器太火了!程序员:知道了快删!

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)

Hands on deep learning (IV) -- convolutional neural network CNN

【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习

Cnopendata list data of Chinese colleges and Universities

Linux server development, SQL statements, indexes, views, stored procedures, triggers

MySQL multi column index (composite index) features and usage scenarios

Sign up now | oar hacker marathon phase III, waiting for your challenge
随机推荐
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
芯片 設計資料下載
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Qt学习26 布局管理综合实例
有 Docker 谁还在自己本地安装 Mysql ?
Linux Installation MySQL 8.0 configuration
json 数据展平pd.json_normalize
青龙面板-今日头条
Installing postgresql11 database under centos7
追风赶月莫停留,平芜尽处是春山
Thinkcmf6.0 installation tutorial
Hands on deep learning (IV) -- convolutional neural network CNN
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
Implementation of replacement function of shell script
Why should we understand the trend of spot gold?
[UTCTF2020]file header
青龙面板--整理能用脚本
Linux server development, MySQL cache strategy
Rust Versus Go(哪种是我的首选语言?)