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 ...
随机推荐
- MySQL语句进行分组后的含有字段拼接方法
MySQL语句: SELECT GROUP_CONCAT(DISTINCT transaction_no) FROM `lm_wh_trans` GROUP BY staff_code; 如果tran ...
- The Implementation of Lua 5.0 阅读笔记(一)
没想到Lua的作者理论水平这么高,这篇文章读的我顿生高屋建瓴之感.云风分享了一篇中译:http://www.codingnow.com/2000/download/The%20Implementati ...
- dedecms:织梦文章如何添加“自定义属性”标签(sql命令行工具)
dede织梦如何添加“自定义属性”标签“症状” 1.进入后台——系统——SQL命令行工具——运行SQL命令行,添加arcatt表字段: insert into`dede_arcatt`(sortid, ...
- python解析smart结构数据
python编程解析如下smart结构数据,得到一行smart信息 run: smartctl -a /dev/sda out: smartctl 6.3 2014-07-26 r3976 [x86_ ...
- 【转】Java中如何遍历Map对
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 万恶的VS2010 快捷键
此随笔用来记录VS的快捷键: 1.ctrl + U :将选定行中的大写置换为小写: 2.ctrl + K,ctrl + C :注释选定行: 3.ctrl + K,ctrl + U :取消注释选定行:
- Android高效加载大图,多图解决方案,有效避免程序OOM异常
收藏自:http://blog.csdn.net/guolin_blog/article/details/9316683 谷歌官方文档:http://developer.android.com/tra ...
- Sobel边缘检测算法(转载)
转载请注明出处: http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰 ...
- 关于SSH整合中对于Hibernate的Session关闭的问题
在web.xml的Struts2的配置上面加上 <filter> <filter-name>OpenSessionInViewFilter</filter-name> ...
- 安卓虚拟机启动后报错: 类似 SDK Manager] Error: Error parsing .....devices.xml 解决方案
昨天用android sdk manager 更新了android sdk, 我是在eclipse上面安装adt来开发android的, 而且我每次打开虚拟机的时候也报错.报错的信息都是一样的. ...