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. 【转】Java中如何遍历Map对

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  2. iOS学习笔记---c语言第九天

    高级指针 指向结构体变量的指针,称为结构体指针 可以使用->指向内容. %p打印地址 void pLenth(cPoint *p1,cPoint *p2) //求两点间的距离  用的开方函数sq ...

  3. Boot Repair-能一键修复ubuntu启动/引导项的软件(告别命令行)

    如果你是windows+ubuntu的双系统,那么重装windows之后(非ghost 方式),是会直接进入windows的,之前的grub引导菜单会被抹掉,想要修复ubuntu的启动菜单的话,通常需 ...

  4. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. C语言学习笔记 -冒泡排序

    //冒泡排序 void main(){ , , , , }; ]); ; i<max - ; i++) { for (int j = i; j<max; j++) { if (a[i]&g ...

  6. js正则函数

    js的正则函数主要有有replace,match,test,search,exec. 首先对文中的变量进行说明: rgExp为包含正则表达式模式和可用标志的正则表达式对象.也可以是包含正则表达式模式和 ...

  7. 1-4-1 Windows应用程序组成及编程步骤

    主要内容:介绍Windows应用程序的组成以及编程步骤 1.应用程序的组成 <1>一个完整的应用程序通常由五种类型的文件组成 1.源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 ...

  8. spark Streaming的Receiver和Direct的优化对比

    Direct 1.简化并行读取:如果要读取多个partition,不需要创建多个输入DStream然后对它们进行union操作.Spark会创建跟Kafka partition一样多的RDD part ...

  9. Android—进度条

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  10. HDU-1520 Anniversary party(树形DP)

    题目大意:一棵树,每个节点都带权.从中取出一些节点,并且子节点不能与父节点同时取,求能取得的最大值. 题目分析:定义状态dp(u,0/1)表示u点不取/取.则状态转移方程为: dp(u,1)=sum( ...