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 ...
随机推荐
- 2 使用unitest 模块扩展功能测试
准备做一个 待办事项清单网站,来演示 Web 开发过程中的所有主要步骤.以及如何在各个步骤中运用TDD理念. ”功能测试“: 从用户的角度查看应用是如何运作的. 从某种程度上可以作为应用的说明书. 作 ...
- 爬取前尘无忧python职位信息并保存到mongo数据库
1.re实现 import re,os import requests from requests.exceptions import RequestException MAX_PAGE = 10 # ...
- jQuery.each(object, [callback])
jQuery.each(object, [callback]) 概述 通用遍历方法,可用于遍历对象和数组.大理石平台检定规程 不同于遍历 jQuery 对象的 $().each() 方法,此方法可用于 ...
- Activiti服务类- FormService服务类
转自:https://www.cnblogs.com/liuqing576598117/p/9814953.html 1.获取//通过流程定义ID获取表单字段集合StartFormData start ...
- Ubuntu: error: snap “phpstorm” has “install-snap” change in progress
Ubuntu: error: snap “phpstorm” has “install-snap” change in progress 投稿日 : 2019-06-10 | カテゴリー : linu ...
- GO111MODULE的设置(及GOPROXY)
环境:win7 go1.13.1 早听说GO111MODULE大名,今天才测试成功,步骤如下: 因为我的Go version >= 1.13,直接用go env -w 设置(注意大小写) go ...
- 玩转orangpi 之frpc远程管理+pcd8544(nokia5110 屏幕) 显示运行状态
玩转orangpi 之frpc远程管理+pcd8544(nokia5110 lcd) 显示运行状态. 物件: orangepi一套(电源,网线,orangepiPC)110元 nokia 5110 l ...
- jenkins之docker安装
此方法安装还存在两个问题1.构建node程序:2.时区问题(在docker run 设置环境变量是否能解决没有试过) 不建议用此方法安装,查看我的其他安装方式 搬运官网步骤,稍微改动. 1.安装doc ...
- Kafka - SASL认证
kafka SASL认证配置 1.找到kafka安装根目录,在config文件夹下创建kafka_server_jaas.conf,写入 KafkaServer { org.apache.kafka. ...
- PHP学习之文件上传类
<?php $up = new Upload(); $newPath = $up->uploadFile('fm'); if ($newPath === false) { var_dump ...