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 ...
随机推荐
- jq实现动态添加样式
<script> $(function(){ $("#list_zlm > a").hover(function(){ $(this).addClass(&quo ...
- 将存储在本地的大量分散的小文件,合并并保存在hdfs文件系统中
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- MariaDB exists 学习
MariaDB exists 学习 exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就 ...
- 大数据hadoop入门学习之集群环境搭建集合
目录: 1.基本工作准备 1.虚拟机准备 2.java 虚拟机-jdk环境配置 3.ssh无密码登录 2.hadoop的安装与配置 3.hbase安装与配置(集成安装zookeeper) 4.zook ...
- 纯代码写UI的时候,如何指定style?
有的时候,需要使用纯代码实现Android UI,那么这个时候如何指定某个UI组件的样式呢? 一般来说,UI组件都有一些set方法可供使用,以调整一些UI属性,从而达到调整样式的目的. 但是,情况并非 ...
- centos 用户组
centos 用户组和用户的添加及所属问题 指定附加组 eg:dy 为组名 ,lisi 为用户,要把lisi 用户添加到 dy 组里面 useradd -G dy lisi 可以同时添加多个组, ...
- 根据域名获取IP地址,并探测是否可达
/* Author :decwang@2014.09.01 Mail :deworks@sina.com*/#define PRINTLOG printf//返回0表示成功,其他为失败. int ge ...
- js如何把字符串转换成json数据的方法
js如何把字符串转换成json数据的方法 function strtojson(str){ var json = eval('(' + str + ')'); return json; } 方法二 f ...
- 软件工程课程作业(三)--四则运算3(C++)
伙伴链接:http://www.cnblogs.com/haoying1994/ 一.设计思路 在此前程序拥有的功能:加减有无负数,除法有无余数以及算式可定制的功能的基础上,此次程序又添加了算式结果的 ...
- DIV的圆角表现和TAB切换
内容大体是从网上找过来的,感觉那位哥们说的对,css还是比较靠谱的,当然有些高玩搞出来的东西本地 没有运行起来. 把自己的应用贴出来同大家分享 <html> <head> &l ...