LintCode "Max Tree"
Something new I learnt from it: what is Treap and a O(n) construction https://en.wikipedia.org/wiki/Cartesian_tree#Efficient_construction
class Solution
{
public:
/**
@param A: Given an integer array with no duplicates.
@return: The root of max tree.
*/
TreeNode* maxTree(vector<int> A) {
size_t n = A.size();
if (!n)
return nullptr; TreeNode *pRoot = nullptr;
stack<TreeNode*> stk; for(auto v : A)
{
TreeNode *pNew = new TreeNode(v);
if(!stk.empty())
{
TreeNode *pLast = stk.top();
if(pNew->val < pLast->val)
{
pLast->right = pNew;
}
else // pNew->val > pLast->val
{
while(!stk.empty())
{
if(stk.top()->val < pNew->val)
{
stk.pop();
}else break;
}
if(!stk.empty())
{
TreeNode *pParent = stk.top();
TreeNode *pTmp = pParent->right;
pParent->right = pNew;
pNew->left = pTmp;
}
else
{
pNew->left = pRoot;
}
}
}
stk.push(pNew); // new node is always right most if(!pRoot || v > pRoot->val)
{
pRoot = pNew;
}
} return pRoot;
}
};
LintCode "Max Tree"的更多相关文章
- Max Tree
Description Given an integer array with no duplicates. A max tree building on this array is defined ...
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- LintCode Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
随机推荐
- 【转】ChainMapper 实例理解一
通过ChainMapper可以将多个map类合并成一个map任务. 下面个这个例子没什么实际意思,但是很好的演示了ChainMapper的作用. 源文件100 tom 90101 mary 85102 ...
- jconsole远程查看jvm性能
Java VisualVM -- JDK自带的强大性能监测工具 这里给出了另一种,可以通过jdk下bin目录下的jconsole.exe来观察java程序内存的使用及变化情况 一般远程调试可以用,性能 ...
- [sql server发布订阅]after触发器执行失败造成复制不成功
结论: (以插入而例) 发布端的数据插入成功 订阅段的数据不会插入 实验 创建一张新表 create table test_subscriber (id int, mark varchar(2),in ...
- iOS学习笔记---oc语言第五天
字典,数组 ,集排序 一.字典类 存储以下数据 name:张三; sex:男;age:18 film:风暴; playcount:10000次;price:60元 字典类用于保存具有映射关系(key- ...
- DEDECMS调用特定ID文章内容的实现方法
DEDECMS调用特定ID文章内容的实现方法 {dede:loop table='dede_addonarticle' sort='aid' row='8' if='aid=524'} [field: ...
- SAP采购订单屏幕增强
转自<http://blog.csdn.net/heng0757/article/details/8073875> 为采购订单增加一个页标签,在其中放入客户自定义字段, 1. CMOD 增 ...
- HDU 5692 线段树+dfs序
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Zuoye for guo
A B C D E A - 3 6 10 13 B - - 5 9 12 C - - - 10 13 D - - - - 9 E - ...
- leetcode 104 Maximum Depth of Binary Tree ----- java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- HTML5学堂 全新的HTML5/前端技术分享平台
HTML5学堂 全新的HTML5/前端技术分享平台 HTML5学堂是做什么的? HTML5学堂~http://www.h5course.com~由多名热爱H5的讲师们组成的一个组织.致力于构建一个前端 ...