leetcode654 Maximum Binary Tree
思路:
使用单调栈可以达到O(n)。
实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums)
{
stack<TreeNode*> st;
for (auto it: nums)
{
TreeNode* tmp = new TreeNode(it), *last = NULL;
while (!st.empty() && tmp->val > st.top()->val)
{
last = st.top(); st.pop();
}
tmp->left = last;
if (!st.empty()) st.top()->right = tmp;
st.push(tmp);
}
TreeNode* res = NULL;
while (!st.empty())
{
res = st.top(); st.pop();
}
return res;
}
}
leetcode654 Maximum Binary Tree的更多相关文章
- 654. Maximum Binary Tree
654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- leetcode_998. Maximum Binary Tree II
https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...
- Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)
Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Swift]LeetCode998. 最大二叉树 II | Maximum Binary Tree II
We are given the root node of a maximum tree: a tree where every node has a value greater than any o ...
- 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序
[抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
随机推荐
- idea jsp文件中body标签内引入编辑器后提示statement expected
解决方案: 1.用标签将script包一层解决问题 2.或者在</script>后添加自闭和标签(推荐) <input/>.<img/> //等等自闭和标签
- Eclipse使用技巧小结
前言:自学Java以来,就一直用eclipse,这款ide深受广大新手和大牛喜爱.学会使用其中的技巧,越用越熟练,开发也就越快捷方便.话不多说,直接上小结吧. 一.快捷键 1.提示 :A|t+/ 2. ...
- C/C++系列之复杂引用
以struct类型为例: 引用 #include"iostream" #include<string> using namespace std; struct myco ...
- [2019HDU多校第一场][HDU 6584][G. Meteor]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6584 题目大意:求所有满足\(0<\frac{p}{q}\leq1, gcd(p,q)=1,p\ ...
- nginx配置及使用
偶尔会用到nginx部署项目,记录nginx配置备忘.主要有端口.地址及别名,代理转发和https配置. 配置文件为nginx.conf. 部署http项目: 1.找到http下的server配置项 ...
- 顺序表Vector
程序中会使用数据结构:例如:顺序表.链表.二叉树: 数据结构在底层中本质上只有两种:数据之间挨着和不挨着: 1.关于Vector
- PHP mysqli_change_user() 函数
改变指定数据库连接的用户: <?php $con=mysqli_connect("localhost","my_user","my_passwo ...
- Activiti--Activity数据库
23张表 ACT_RE_资源库流程规划表 act_re_deployment 部署信息表 act_re_model 流程设计模型部署表 act_re_procdef 流程定义数据表 ACT_RU_运行 ...
- vue中插槽(slot)的使用
刚学vue的时候,曾经学习过slot插槽的使用,但是后面接触的不多,因为之前我还没使用element-ui... 但是使用了element-ui之后,里面的许多组件,有时候会使用插槽,为了巩固一下插槽 ...
- 页面的beforeunload和unload的事件应用
博主最近遇到一个需求,需要在用户离开之前给一个提示,是否确认离开,并且用户确认离开的话,需要发出一个请求 下面直接上代码: <!DOCTYPE HTML> <html> < ...