当前位置:网站首页>Leetcode brush questions: binary tree 18 (largest binary tree)
Leetcode brush questions: binary tree 18 (largest binary tree)
2022-07-05 20:03:00 【Taotao can't learn English】
654. The largest binary tree
Given an array of integers without repeating elements . A maximum binary tree constructed from this array is defined as follows :
- The root of a binary tree is the largest element in the array .
- The left subtree is the largest binary tree constructed from the left part of the maximum value in the array .
- Right subtree is the largest binary tree constructed by the right part of the maximum value in the array .
Build the largest binary tree from a given array , And output the root node of the tree .
Example :
Tips :
The size of the given array is [1, 1000] Between .
Traverse , Find the maximum value and its position . The array is divided into left and right subtree arrays . Create nodes and add left and right subtrees . The recursive termination condition is that the left and right arrays are empty , That is, the leaf node .
package com.programmercarl.tree;
/** * @ClassName ConstructMaximumBinaryTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 15:45 * @Version 1.0 * 654. The largest binary tree * 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;
}
}
// Find the current maximum
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});
}
}
边栏推荐
- 期货如何网上开户?安不安全?
- 秋招字节面试官问你还有什么问题?其实你已经踩雷了
- openh264解码数据流向分析
- 浅浅的谈一下ThreadLocalInsecureRandom
- The city chain technology Digital Innovation Strategy Summit was successfully held
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
- Flume series: interceptor filtering data
- webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
- 解决php无法将string转换为json的办法
- Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
猜你喜欢
Add data to excel small and medium-sized cases through poi
Redis cluster simulated message queue
. Net distributed transaction and landing solution
.Net分布式事务及落地解决方案
Build your own website (16)
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Parler de threadlocal insecurerandom
95后阿里P7晒出工资单:狠补了这个,真香...
Interviewer: what is the internal implementation of set data types in redis?
随机推荐
再忙不能忘安全
C application interface development foundation - form control (5) - grouping control
通配符选择器
淺淺的談一下ThreadLocalInsecureRandom
【obs】libobs-winrt :CreateDispatcherQueueController
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Tasks in GStreamer
Is it safe for Galaxy Securities to open an account online?
JVMRandom不可设置种子|问题追溯|源码追溯
函数的概念及语法
ICTCLAS用的字Lucene4.9捆绑
How to retrieve the root password of MySQL if you forget it
[C language] three implementations of quick sorting and optimization details
【无标题】
Go language | 02 for loop and the use of common functions
【c语言】归并排序
Thread pool parameters and reasonable settings
Let's talk about threadlocalinsecurerandom
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
Go language | 01 wsl+vscode environment construction pit avoidance Guide