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"的更多相关文章

  1. Max Tree

    Description Given an integer array with no duplicates. A max tree building on this array is defined ...

  2. [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 ...

  3. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  4. 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. ...

  5. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  6. 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 ...

  7. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  8. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  9. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

随机推荐

  1. 服务调用方案(Spring Http Invoker) - 我们到底能走多远系列(40)

    我们到底能走多远系列(40) 扯淡:  判断是否加可以效力于这家公司,一个很好的判断是,接触下这公司工作几年的员工,了解下生活工作状态,这就是你几年后的状态,如果满意就可以考虑加入了. 主题: 场景: ...

  2. php防注入留言板(simple)

    新手学php,试手案例便是留言板.以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视). 最近学习了php访问MySQL数据库的一些知识,重写了一下留言 ...

  3. c# 多语言实现 与 InitializeCulture

    在实现多语言的时候,我们会使用GetGlobalResourceObject,在服务器控件中显示多语言文本要使用<%$ Resources:Common, Export %>, 但是在设置 ...

  4. ANTLR3完全参考指南读书笔记[01]

    引用 Terence Parr. The Definitive ANTLR Reference, Building Domain Specific Languages(antlr3 version). ...

  5. html5之我見

    大多數知道html5的國人,不限於IT業內人員,對Html5存在較大誤解. 幾天前在新浪微博看到一個ID為"黑客師"的微博發佈了一張照片,名為"小白與高手的差別" ...

  6. window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马

    网站安全中,对目录的执行权限是非常敏感的,一般来说,可以写入的目录是不能够拥有脚本的执行权限的,可写入的目录如: data.uploads,data目录主要是基本配置文件和缓存数据,uploads则是 ...

  7. c 函数及指针学习 7

    1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...

  8. PHP实例开发(2)PHP通过mail()或Socket发邮件

    PHP通过mail()或Socket发邮件 1.PHP中发送邮件的方法 PHP发送邮件是“非常的简单” 因为他提供了mail()函数直接发送,但这也继 register globals 成为了对初学者 ...

  9. tomcat 启动时内存溢出

    在tomcat_home/bin目录下找到catalina.bat,用文本编辑器打开,加上下面一行: set JAVA_OPTS= -Xms1024M -Xmx1024M -XX:PermSize=2 ...

  10. php配置伪静态的方法

    mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面.下面我详细说说它的使用方法 .检测Apache是否支持mod_rewrite 通过php提供的phpinfo()函数查看 ...