Max Tree
Description
Given an integer array with no duplicates. A max tree building on this array is defined as follow:
- The root is the maximum number in the array
- The left subtree and right subtree are the max trees of the subarray divided by the root number.
Construct the max tree by the given array.
思路:利用数组实现基本数据结构的调整,当前遍历到的数字比stack中的最后一个大时,将stk中的最后一个数字转变为当前节点的左子树,循环调整至stack为空或者stack中的最后节点值大于新节点的值。如果stack不为空,说明stack中的最后一个节点值大于新节点值,则将新节点设为stack中的最后一个节点的右子树,将新节点存入stack。
public class Solution {
/**
* @param A
* : Given an integer array with no duplicates.
* @return: The root of max tree.
*/
public static TreeNode maxTree(int[] A) {
// write your code here
Stack<TreeNode> stack = new Stack<TreeNode>(); //申请栈存放节点
TreeNode root = null;
for (int i = 0; i <= A.length; i++) {
TreeNode right = i == A.length ? new TreeNode(Integer.MAX_VALUE) //如果i==length,新建节点设置值为无穷大,否则值为A[i]
: new TreeNode(A[i]);
while (!stack.isEmpty()) { //如果栈不为空
if (right.val > stack.peek().val) { //如果新建节点的值比栈顶大
TreeNode nodeNow = stack.pop(); //临时保存栈顶节点并弹出
if (stack.isEmpty()) { //如果栈为空
right.left = nodeNow; //临时保存的栈顶的节点是当前新建节点的左子树
} else {
TreeNode left = stack.peek();
if (left.val > right.val) {
right.left = nodeNow; //新建节点的左子树为临时保存节点
} else {
left.right = nodeNow; //当前栈顶的节点的右子树为新建节点
}
}
} else
break;
}
stack.push(right); //将新建节点压入栈中
}
return stack.peek().left;
}
}
Max Tree的更多相关文章
- LintCode "Max Tree"
Something new I learnt from it: what is Treap and a O(n) construction https://en.wikipedia.org/wiki/ ...
- SPOJ 375 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ 3237:Tree(树链剖分)
http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...
- codeforces 675D D. Tree Construction(线段树+BTS)
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
随机推荐
- JUC之AbstractQueuedSynchronizer原理分析 - 独占/共享模式
1. 简介 AbstractQueuedSynchronizer (抽象队列同步器,以下简称 AQS)出现在 JDK 1.5 中,由大师 Doug Lea 所创作.AQS 是很多同步器的基础框架. R ...
- WUSTOJ 1299: 结点选择(Java)
题目链接:
- Echarts设置y轴值间隔 以及设置 barWidth : 30,//柱图宽度
需求:如图,y轴之间的距离太小,这样就太过于拥挤了,现在要修改echarts里面的属性,设置y轴值间隔让图表看上去舒服一些. 其实很多问题,真的只是因为自己没有好好的看文档,很多文档上面都写的 ...
- 前端require代码抽离小技巧
DEMO 文件目录结构 plugin.js // /CommonJS规范 // var exports = module.exports; exports.test = function () { c ...
- Django model反向关联名称的方法(转)
原文:https://www.jb51.net/article/152825.htm
- MSMQ消息队列,包括远程访问
之前的项目用到了队列,现在总结一下,下面有非常详细的DEMO,希望能对有需要的人提供帮助. 使用场景:在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了 ...
- google mock C++单元测试框架
转:google mock C++单元测试框架 2012-03-12 09:33:59 http://blog.chinaunix.net/uid-25748718-id-3129590.html G ...
- 用scrapy爬取京东商城的商品信息
软件环境: gevent (1.2.2) greenlet (0.4.12) lxml (4.1.1) pymongo (3.6.0) pyOpenSSL (17.5.0) requests (2.1 ...
- react-native 沉浸式状态栏
使用StatusBar即可实现沉浸式,但是必须把背景色设置为透明.否则如果有不同页面的头部颜色不一样的话,导航栏的颜色动画会很怪异,不会是跟着页面一起动画. <StatusBar barStyl ...
- iPhone电话与短信相关代码小结
关于iPhone上电话与短信相关功能,做一个简单总结: 使用公开SDK能实现的功能: (1)获取和操作通讯录.使用函数 ABAddressBookRequestAccessWithCompletion ...