当前位置:网站首页>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});
}
}
边栏推荐
- 毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
- Base du réseau neuronal de convolution d'apprentissage profond (CNN)
- Is it safe for Guohai Securities to open an account online?
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- Thread pool parameters and reasonable settings
- Common operators and operator priority
- aggregate
- MMO項目學習一:預熱
- How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
- Force buckle 729 My schedule I
猜你喜欢
IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
[AI framework basic technology] automatic derivation mechanism (autograd)
How MySQL queries and modifies JSON data
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
测试外包公司怎么样?
SecureRandom那些事|真伪随机数
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
C#应用程序界面开发基础——窗体控制(5)——分组类控件
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
测试的核心价值到底是什么?
随机推荐
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Android interview classic, 2022 Android interview written examination summary
Postman核心功能解析-参数化和测试报告
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
How to safely and quickly migrate from CentOS to openeuler
Interviewer: what is the internal implementation of set data types in redis?
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
Postman core function analysis - parameterization and test report
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
gst-launch的-v参数
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Xaas trap: all things serve (possible) is not what it really needs
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
Necessary skills for interview in large factories, 2022android will not die, I will not fall
Django uses mysqlclient service to connect and write to the database