当前位置:网站首页>Binary tree - 654. Maximum binary tree
Binary tree - 654. Maximum binary tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given an array of non repeating integers nums. The maximum binary tree can be obtained from nums Build... Recursively :
1. Create a root node , Its value is nums Maximum of .
2. Recursively construct the left subtree on the prefix of the subarray to the left of the maximum .3. Recursively construct the right subtree on the subarray suffix to the right of the maximum . return num5 Maximum binary tree constructed .
2 Title Example
Input :nums = [3,2,1,6,0,5]
Output :[6,3,5,null,2,0,null,null,1]
explain : The recursive call looks like this :
- [3,2,1,6,0,5] The maximum value in is 6 , The left part is [3,2,1] , The right part is [0,5] .
- [3,2,1] The maximum value in is 3 , The left part is [] , The right part is [2,1] .
- An empty array , No child nodes .
- [2,1] The maximum value in is 2 , The left part is [] , The right part is [1] .
- An empty array , No child nodes .
- There's only one element , So the child node is a value of 1 The node of .
- [0,5] The maximum value in is 5 , The left part is [0] , The right part is [] .
- There's only one element , So the child node is a value of 0 The node of .
- An empty array , No child nodes .
- [3,2,1] The maximum value in is 3 , The left part is [] , The right part is [2,1] .
Topic example source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-binary-tree
3 Topic tips
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
nums All integers in Different from each other
4 Ideas
Method 1 : recursive
This method is very simple . Create method construct(nums,1,r), Used to find in the array nums In the from l To , Indexes ( It doesn't include , A place ) The root node of the largest binary tree in .
The algorithm steps are as follows :
- First call construct(nums,0, n), among n It's an array nums
The length of . - In index range (l : r―1) Index to find the maximum value in , take nums[max_ department ] As root node .
- call construct(nums,1,max_i) Create the left child of the root node . Perform this operation recursively , Create the entire left subtree of the root node .
- Allied , call construct(nums,max_i + 1,r) Create the right subtree of the root node .
- Method construct(nums,0, n) Returns the root node of the largest binary tree .
Complexity analysis
. Time complexity :o(n²). Method construct— Called together n Time . Every time you recursively find the root node , You need to traverse all elements in the current index range to find the maximum value . In general , The complexity of each traversal is O(log n), The total complexity is o(n logn). At worst , Array Tums Orderly , The total complexity is O(n²).
. Spatial complexity :O(n). Recursive call depth is n. On average , The length is n The recursive call depth of the array is O(log n).
5 My answer
public class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums, 0, nums.length);
}
public TreeNode construct(int[] nums, int l, int r) {
if (l == r)
return null;
int max_i = max(nums, l, r);
TreeNode root = new TreeNode(nums[max_i]);
root.left = construct(nums, l, max_i);
root.right = construct(nums, max_i + 1, r);
return root;
}
public int max(int[] nums, int l, int r) {
int max_i = l;
for (int i = l; i < r; i++) {
if (nums[max_i] < nums[i])
max_i = i;
}
return max_i;
}
}
边栏推荐
- Observer model of behavioral model
- 回溯——17. 电话号码的字母组合
- 程序环境和预处理
- Binary tree -- 111. Minimum depth of binary tree
- SQLZOO——Nobel Quiz
- 牛市还没有结束,还有下半场 2021-05-18
- [ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
- Prometheus 运维工具 Promtool (二) Query 功能
- 二叉树——654. 最大二叉树
- After entering www.baidu.com in the address bar
猜你喜欢

SQLZOO——Nobel Quiz

MySQL的DDL、DML和DQL的基本语法
![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
[learning notes] unreal 4 engine introduction (IV)

Stack and queue - 239. Sliding window maximum

Leetcode169-多数元素详解

07_ue4进阶_发射火球扣mp值和攻击扣血机制

Lua script Wireshark plug-in to resolve third-party private protocols

“动物币”凶猛,陷阱还是机遇?2021-05-12

LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果

C语言实战之猜拳游戏
随机推荐
Dead letter queue and message TTL expiration code
最近随感,关于牛市和DeFi 2021-05-17
二叉树——226. 翻转二叉树
Annotation @autowired source code analysis
LeetCode 沙胡同系列 -- 63. 不同路径 II
MySQL的DDL、DML和DQL的基本语法
Song of statistics lyrics
Responsibility chain model of behavioral model
C language (high level) program environment and preprocessing
行为型模式之迭代器模式
Yolov3 trains its own data set
Leetcode169-多数元素详解
寻找链表的中间节点
J9数字论:什么是DAO模式?DAO发展过程的阻碍
[learning notes] unreal 4 engine introduction (III)
安全文档归档软件
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
什么是奇偶校验?如何用C语言实现?
二叉树——110. 平衡二叉树
VSCode格式化Json文件