最后更新

08-Jan-2017

开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的。。。。。

一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用post-order traversal来构建。

public class Solution {

    public SegmentTreeNode build(int[] A) {
// write your code here
if (A.length == 0) return null;
return buildTree(A, 0, A.length - 1);
} public SegmentTreeNode buildTree(int[] A, int left, int right) {
if (left > right) return null;
if (left == right) return new SegmentTreeNode(left, right, A[left]);
int mid = left + (right - left) / 2; SegmentTreeNode leftChild = buildTree(A, left, mid);
SegmentTreeNode rightChild = buildTree(A, mid + 1, right); SegmentTreeNode tempRoot = new SegmentTreeNode(left, right, Math.max(leftChild.max, rightChild.max));
tempRoot.left = leftChild;
tempRoot.right = rightChild;
return tempRoot; }
}

439. Segment Tree Build II的更多相关文章

  1. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  2. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  3. lintcode :Segmemt Tree Build II

    题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...

  4. Lintcode247 Segment Tree Query II solution 题解

    [题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...

  5. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  6. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  7. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  8. 201. Segment Tree Build

    最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...

  9. 247. Segment Tree Query II

    最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...

随机推荐

  1. POJ3485 区间问题

    题目描述有些坑.. 题意: 有一条高速公路在x轴上,从(0,0)到(L,0).周围有一些村庄,希望能够在高速公路上开通几个出口,使得每个村庄到最近的出口距离小于D,求出最少需要开通多少个出口. 解题思 ...

  2. overload和override

    Overload是重载的意思,Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 重写Ove ...

  3. UVa 1636 (概率) Headshot

    既然是第一道概率题,就正儿八经地分析一下吧. 题意: 有一个左轮枪,里面随机装了或者没装子弹,用一个01序列表示.现在已知扣动第一次扳机没有子弹,问是继续扣动扳机还是随机转动一下再扣,那种选择使得第二 ...

  4. 安装IIS之后运行aspx 显示“服务器应用程序不可用” 解决办法

    引起这个的原因大概是现安装了.Net Framework,后装的IIS导致.Net没有在IIS里注册.  另外,还有可能是ASPNET账户没有IIS所指定服务器目录的权限.在资源管理器中找到“工具-文 ...

  5. Java Web编程的主要组件技术——MVC设计模式

    参考书籍:<J2EE开源编程精要15讲> MVC(Model View Controller),Model(模型)表示业务逻辑层,View(视图)代表表述层,Controller(控制)表 ...

  6. Android-给另一个Activity传递HashMap

    I have a HashMap which I would pass to another Activity class. I simply use this code: Intent intent ...

  7. XA事务处理

    XA接口详解 X/Open XA接口是双向的系统接口,在事务管理器(Transaction Manager)以及一个或多个资源管理器(Resource Manager)之间形成通信桥梁.事务管理器控制 ...

  8. Another Crisis

    题意: 给出一个树,当孩子节点为1的数量占孩子总数的T%时父节点变成1,求使根节点变成1需要叶子节点为1的最小数量. 分析: 简单的树状dp,dp[i]以i为根的子树所需的最小数量,取它所有子树中最小 ...

  9. POJ 3321- Apple Tree(标号+BIT)

    题意: 给你一棵树,初始各节点有一个苹果,给出两种操作,C x 表示若x节点有苹果拿掉,无苹果就长一个. Q x查询以x为根的子树中有多少个苹果. 分析: 开始这个题无从下手,祖先由孩子的标号不能确定 ...

  10. 使用Redis的理由

    Redis是一个远程内存数据库,它不仅性能强劲,而且还具有复制特性以及为解决问题而生的独一无二的数据模型.Redis提供了5种不同类型的数据结构,各式各样的问题都可以很自然地映射到这些数据结构上:Re ...