当前位置:网站首页>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;
}
}
边栏推荐
- 【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
- JSON data flattening pd json_ normalize
- 芯片资料 网站 易特创芯
- [SUCTF 2019]Game
- OpenJudge NOI 2.1 1752:鸡兔同笼
- Redis technology leak detection and filling (II) - expired deletion strategy
- 太真实了,原来自己一直没有富裕起来是有原因的
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- Leetcode 40: combined sum II
- LeetCode 90:子集 II
猜你喜欢
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Linux server development, SQL statements, indexes, views, stored procedures, triggers
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Qt学习28 主窗口中的工具栏
Problem solving: unable to connect to redis
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Content of string
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
JSON data flattening pd json_ normalize
随机推荐
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
[SUCTF 2019]Game
Qt学习27 应用程序中的主窗口
Cnopendata American Golden Globe Award winning data
Installing postgresql11 database under centos7
Qt学习28 主窗口中的工具栏
The principle and implementation of buffer playback of large video files
央视太暖心了,手把手教你写HR最喜欢的简历
Why should we understand the trend of spot gold?
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
JS quick start (I)
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
misc ez_ usb
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Linux server development, redis protocol and asynchronous mode
A bit of knowledge - about Apple Certified MFI
Topic not received? Try this
C language flight booking system
[mathematical notes] radian
3D reconstruction - stereo correction