当前位置:网站首页>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});
}
}
边栏推荐
- Ffplay document [easy to understand]
- Some problems encountered in cocos2d-x project summary
- MySql的root密码忘记该怎么找回
- id选择器和类选择器的区别
- c语言oj得pe,ACM入门之OJ~
- Relationship between floating elements and parent and brother boxes
- selenium 元素信息
- 中金财富在网上开户安全吗?
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- 图嵌入Graph embedding学习笔记
猜你喜欢
Android interview classic, 2022 Android interview written examination summary
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
Using repositoryprovider to simplify the value passing of parent-child components
Database logic processing function
Interviewer: what is the internal implementation of set data types in redis?
【无标题】
2023年深圳市绿色低碳产业扶持计划申报指南
Go language | 01 wsl+vscode environment construction pit avoidance Guide
leetcode刷题:二叉树13(相同的树)
秋招字节面试官问你还有什么问题?其实你已经踩雷了
随机推荐
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
Is it safe to open a mobile stock account? Is it reliable?
【obs】libobs-winrt :CreateDispatcherQueueController
Interviewer: what is the internal implementation of set data types in redis?
Zero cloud new UI design
银河证券在网上开户安全吗?
Summer Challenge harmonyos - realize message notification function
微信小程序正则表达式提取链接
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Some problems encountered in cocos2d-x project summary
ICTCLAS用的字Lucene4.9捆绑
gst-launch的-v参数
Database logic processing function
Tasks in GStreamer
DP:树DP
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
leetcode刷题:二叉树13(相同的树)
C langue OJ obtenir PE, ACM démarrer OJ
线程池参数及合理设置
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer