654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序
[抄题]:
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree: 6
/ \
3 5
\ /
2 0
\
1
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
TreeNode返回空值对应的是null
[思维问题]:
不知道dc怎么写:recursion的条件是start end就行了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- helper函数中建了节点,很显然主函数中就不用再建了
- buildtree必须写root.left root.right
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
buildtree必须写root.left root.right,recursion的条件是start end就行了。
[复杂度]:Time complexity: O(nlgn) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
//corner case
if (nums == null || nums.length == 0) return null; //utilize
return constructHelper(nums, 0, nums.length - 1);
} public TreeNode constructHelper(int[] nums, int start, int end) {
//exit requirement
if (start > end) return null; //find the max
int maxIdx = start;
for (int i = start + 1; i <= end; i++) {
if (nums[i] > nums[maxIdx])
maxIdx = i;
}
TreeNode root = new TreeNode(nums[maxIdx]); //recursive in left and right
root.left = constructHelper(nums, start, maxIdx - 1);
root.right = constructHelper(nums, maxIdx + 1, end); //return
return root;
}
}
654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序的更多相关文章
- 654. Maximum Binary Tree
654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...
- LeetCode 654. Maximum Binary Tree最大二叉树 (C++)
题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- 654. Maximum Binary Tree最大二叉树
网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [LeetCode] 654. Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- Python 解LeetCode:654. Maximum Binary Tree
用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...
- [LeetCode]654. Maximum Binary Tree最大堆二叉树
每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...
随机推荐
- 【python接口自动化框架-unittest】【一】unittest单元测试框架概念
一.unittst单元测试框架 概念参考:https://docs.python.org/2/library/unittest.html 使用方法:import unittest (引入unittes ...
- devops的理解
DevOps(Development和Operations的组合词)是一种重视"软件开发人员(Dev)"和"IT运维技术人员(Ops)" DevOps是一组过程 ...
- win32网络模型之重叠I/O
网上大部分重叠I/O的基本概念都讲得很清楚,但是大多讲得不是很深入,实际用起来很多问题.这里只对完成实例的通知进行讨论,对问题进行总结. 重叠IO异步读写后,在某一时刻"完成"后会 ...
- Python生成器(generator)和迭代器(Iterator)
列表生成式 a = [i+1 for i in range(10)] print(a) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 这就是列表生成式 生成器(generator) ...
- bootstrap modal 移除数据
有时业务需求,在弹出模态框时需要动态的展示,但是不做处理时,每次弹出的都是第一次的数据,所以需要监听模态框关闭,每次关闭需要先清除里面的数据. //监听弹页关闭 $("#myModal&qu ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- keepalived 高可用配置
下载地址:http://www.keepalived.org/software/keepalived-1.2.12.tar.gzht 安装方法:1. ./configure 可能出现的错误 !!! O ...
- jenkins使用(ubuntu16.0环境)
本文总结了使用jenkins过程.大部分是网上链接,以后自已查看使用. ssh远程链接服务器 检查是否开启ssh ps -ef|grep ssh 1.安装ssh 2.开启root用户 3.充许ro ...
- 执行sql语句为什么?用PreparedStatement要比Statement好用
PreparedStatement public interface PreparedStatement extends Statement;可以看到PreparedStatement是Stateme ...
- Python面向过程编程
面向过程编程 D:\Document\视频\python20期\day4\视频\面向过程编程 三元表达式示例1 #三元表达式x=10 y=20 res=x if x>y else y print ...