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 ...
 
随机推荐
- Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)
			
目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...
 - Markdown试试
			
from os import time print("haha") from os import time print("haha") time.time()! ...
 - Go 实现短 url 项目
			
首先说一下这种业务的应用场景: 把一个长 url 转换为一个短 url 网址 主要用于微博,二维码,等有字数限制的场景 主要实现的功能分析: 把长 url 地址转换为短 url 地址 通过短 url ...
 - Nginx快速自查手册
			
本项目是一个 Nginx 极简教程,目的在于帮助新手快速入门 Nginx. demos 目录中的示例模拟了工作中的一些常用实战场景,并且都可以通过脚本一键式启动,让您可以快速看到演示效果. 概述 什么 ...
 - 基于【 bug解决】一 || mysql的ONLY_FULL_GROUP_BY导致的sql语句错误
			
一.Mysql错误: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated ...
 - Tomcat安装及环境配置
			
欢迎任何形式的转载,但请务必注明出处. 本章内容 安装 环境变量入口 三个系统变量配置 测试安装配置是否成功 安装之前请安装jdk并进行环境配置(点击进入jdk教程) 一.安装 点击进入官网下载 二. ...
 - TR-TR模块资料汇总
			
转载: TR模块培训 https://www.docin.com/p-1704805923.html 现金管理(Cash Management)和预算控制(Cash Budget Management ...
 - 关于INT_MIN
			
来自为知笔记(Wiz)
 - 后台对象转JSON字符串传到前台,前台JSON字符串转对象绑定标签赋值
			
/// <summary> /// 创建换货单-自动获取对应的数据(后端) /// </summary> [HttpGet] public ActionResult GetPr ...
 - unittest 运行slenium(四)---通过指定用例的形式运行用例
			
一: 说明 跟数据驱动唯一的区别为用例数据获取时,及运行方式不同. 其它都基本相同,可参考https://www.cnblogs.com/xiaodingdong/p/11753220.html 二: ...