leetcode654
class Solution
{
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums)
{
if (nums.size() == )
return NULL;
else if (nums.size() == )
{
TreeNode * node = new TreeNode(nums[]);
return node;
}
else
{
int max = ;
for (int i = ; i<nums.size(); i++)
{
if (nums[i]>nums[max])
max = i;
}
vector<int> left_num, right_num;
for (int i = ; i<max; i++)
{
left_num.push_back(nums[i]);
}
for (int j = max + ; j<nums.size(); j++)
{
right_num.push_back(nums[j]);
}
TreeNode * node = new TreeNode(nums[max]);
node->left = constructMaximumBinaryTree(left_num);
node->right = constructMaximumBinaryTree(right_num);
return node;
}
}
};
leetcode654的更多相关文章
- [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- leetcode654 Maximum Binary Tree
思路: 使用单调栈可以达到O(n). 实现: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- Homebrew/Linuxbrew 安装常有工具
Homebrew https://brew.sh/ Install: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent ...
- The web application you are attempting to access on this web server is currently unavailable.......
今天去服务器安装了个.net 4.0 framework(原本有1.0和2.0的),配置好站点后,选择版本为4.0,访问出错,错误代码如下 Server Application Unavailable ...
- MyBatise代码自动生成时候Oralce的number类型BigDecimal问题
使用MyBatise的代码自动生成工具时候,即便在配置文件中定义了 <javaTypeResolver> <property name="forceBigDecimals& ...
- BZOJ1555 KD之死
如果没有必选的限制条件,就是水题了... 只要按照w + t排序就可以了,然后搞个堆来维护 于是有了限制条件,还是水题... 到了必选的时候强制选上,不加入堆中即可. /*************** ...
- OCX组件
转自:http://blog.sina.com.cn/s/blog_4ca9ceef0100ixzb.html 一.OCX(OLE Control Extensio,OLE Object Linkin ...
- SQLServer查看用户连接数
SELECT login_name, ) user_count FROM Sys.dm_exec_requests dr WITH(nolock) RIGHT OUTER JOIN Sys.dm_ex ...
- Alpha冲刺一 (6/10)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...
- ionic2中跨页面回传值
1.在跳转到新页面时传入一个contactsCallback的参数,在该参数的函数定义中做出一个承诺. 注意:最开始我本来是采用如下图方式的,但是很不幸,出现了问题,问题所在就是关于这个this的作用 ...
- BZOJ3295: [Cqoi2011]动态逆序对(树状数组套主席树)
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7465 Solved: 2662[Submit][Sta ...
- Codeforces 1009D:Relatively Prime Graph
D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...