当前位置:网站首页>leetcode刷题:二叉树18(最大二叉树)
leetcode刷题:二叉树18(最大二叉树)
2022-07-05 19:52:00 【涛涛英语学不进去】
654.最大二叉树
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
- 二叉树的根是数组中的最大元素。
- 左子树是通过数组中最大值左边部分构造出的最大二叉树。
- 右子树是通过数组中最大值右边部分构造出的最大二叉树。
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
示例 :
提示:
给定的数组的大小在 [1, 1000] 之间。
遍历,找最大值和其位置。数组分割为左右子树数组。创建结点添加左右子树。递归终止条件为左右数组都为空,即为叶子结点。
package com.programmercarl.tree;
/** * @ClassName ConstructMaximumBinaryTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 15:45 * @Version 1.0 * 654. 最大二叉树 * https://leetcode.cn/problems/maximum-binary-tree/ **/
public class ConstructMaximumBinaryTree {
public TreeNode constructMaximumBinaryTree(int[] nums) {
if (nums.length == 0) {
return null;
}
int max = -1;
int maxIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
maxIndex = i;
}
}
//找到当前最大值
TreeNode root = new TreeNode(nums[maxIndex]);
// 6 - 3 = 3
int[] leftNums = new int[maxIndex];
// 6 - 3 - 1 = 2
int[] rightNums = new int[nums.length - maxIndex - 1];
for (int i = 0; i < nums.length; i++) {
if (i < maxIndex) {
leftNums[i] = nums[i];
} else if (i > maxIndex) {
rightNums[i - maxIndex - 1] = nums[i];
}
}
// System.out.println(leftNums);
// System.out.println(rightNums);
root.left = constructMaximumBinaryTree(leftNums);
root.right = constructMaximumBinaryTree(rightNums);
return root;
}
public static void main(String[] args) {
new ConstructMaximumBinaryTree().constructMaximumBinaryTree(new int[]{
3,2,1,6,0,5});
}
}
边栏推荐
- 95后阿里P7晒出工资单:狠补了这个,真香...
- 使用 RepositoryProvider简化父子组件的传值
- Necessary skills for interview in large factories, 2022android will not die, I will not fall
- 浮动元素与父级、兄弟盒子的关系
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
- Android interview, Android audio and video development
- 爬虫练习题(二)
- Securerandom things | true and false random numbers
- 多分支结构
- The difference between ID selector and class selector
猜你喜欢
Do you know several assertion methods commonly used by JMeter?
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
MMO项目学习一:预热
IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
aggregate
深度学习 卷积神经网络(CNN)基础
Redis cluster simulated message queue
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
随机推荐
gst-launch的-v参数
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
建立自己的网站(16)
The city chain technology Digital Innovation Strategy Summit was successfully held
Common operators and operator priority
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
通配符选择器
JVMRandom不可设置种子|问题追溯|源码追溯
Gstreamer中的task
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
测试外包公司怎么样?
软件测试是干什么的?学习有啥要求?
安卓面试宝典,2022Android面试笔试总结
What do software test engineers do? How about the prospect of treatment?
Do you know several assertion methods commonly used by JMeter?
Fundamentals of shell programming (Part 8: branch statements -case in)
建议收藏,我的腾讯Android面试经历分享
【obs】libobs-winrt :CreateDispatcherQueueController
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform