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 ...
随机推荐
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
- 关于epoll,select,poll的理解
select: 轮询+fd_set 1.采用fd_set存储fd(fd_set通过数组位图实现) 2.每次调用select,都需要把fd集合从用户态拷贝到内核态,fd越多开销越大 3.每次调用sele ...
- IDEA 的操作与使用
idea 设置syso File –> Setting –> Editor –> Live Templates debug 调试: F7 在 Debug 模式下,进入下一步,如果当前 ...
- [转帖]谷歌宣称首次实现量子优越性,IBM“不服”,中国同行咋看?
谷歌宣称首次实现量子优越性,IBM“不服”,中国同行咋看? 投递人 itwriter 发布于 2019-10-24 15:46 评论(7) 有306人阅读 原文链接 [收藏] « » https:// ...
- [转帖]时间序列数据库 (TSDB)
时间序列数据库 (TSDB) https://www.jianshu.com/p/31afb8492eff 0.3392019.01.28 10:51:33字数 5598阅读 4030 背景 2017 ...
- 在 Docker 中手工部署 ASP.NET Core 应用
另一篇:在 Visual Studio 中部署 ASP.NET Core 应用 操作步骤 1. 安装 Docker For Windows(安装之前 Windows 需要开启 Hyper-V 虚拟机 ...
- @Scheduled注解各参数详解
@Scheduled注解各参数详解 @Scheduled注解的使用可以参考这个:https://www.cnblogs.com/mengw/p/11564338.html 参数详解 1. cron 该 ...
- python 之 并发编程(生产者消费者模型、守护进程的应用)
9.8 生产者消费者模型 该模型中包含两类重要的角色: 1.生产者:将负责造数据的任务比喻为生产者 2.消费者:接收生产者造出的数据来做进一步的处理的被比喻成消费者 实现生产者消费者模型三要素:1.生 ...
- Linux or Mac 重启网络
Mac sudo ifconfig en0 down sudo ifconfig en0 up Linux /etc/init.d/networking restart
- Arm-Linux 移植 alsa
ref : https://www.cnblogs.com/yutingliuyl/p/6718875.html https://blog.csdn.net/yuanxinfei920/article ...