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 ...
随机推荐
- pytorch1.0实现GAN
import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt # 超参数设置 # Hype ...
- HDU 1016Presentation Error
这是一道典型的DFS题目.幻想有n个箱子,每次都向箱子里扔一个数,(当然第一个是必定是1,因为题目要求按字典序输出).判断输出的条件就是,当我移动到第n+1个箱子的时候,就要return了,当然还要判 ...
- C++程序的多文件组成
C++程序的多文件组成 [例3.32] 一个源程序按照结构划分为3个文件 // 文件1 student.h (类的声明部分) #include<iostream.h> #include&l ...
- Python--拦截接口
- PAT(B) 1052 卖个萌(Java:0分 待解决,C:20分)
题目链接:1052 卖个萌 (20 point(s)) 题目描述 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们 ...
- 【HC89S003F4开发板】 8c转义成汇编工程
HC89S003F4开发板建立汇编工程 选择编译文件 @选用开发板闪灯例程,将例程删除多余的注释,后面生成的文件会更直观. #define ALLOCATE_EXTERN #include " ...
- python检测挖矿特征的几种方式
电脑性能上: ①cpu和内存使用率(常见): python 实时得到cpu和内存的使用情况方法_python_脚本之家https://www.jb51.net/article/141835.htm ② ...
- 题解-AtCoder ARC-078F Mole and Abandoned Mine
problem ATC-arc078F 题意概要:给定一个 \(n\) 点 \(m\) 边简单无向图(无自环无重边),边有费用,现切去若干条边,使得从 \(1\) 到 \(n\) 有且仅有一条简单路径 ...
- Vuex 刷新后数据丢失问题 Typescript
问题描述:Vuex保存的数据在页面刷新后会全部丢失清除 问题解决方案:使用sessionstorage进行保存,在页面刷新时保存至sessionStorage,页面在加载时再进行填充 (另有vue ...
- Go 关于 kafka 的生产者、消费者实例
zookeeper + kafka 首先要在 apche 官网下载 kafka 的程序包(linux版本),然后放到服务器上解压,得到以下目录 bin 目录下包含了服务的启动脚本 启动 zookeep ...