439. Segment Tree Build II
最后更新
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的更多相关文章
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- 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 ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 201. Segment Tree Build
最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...
- 247. Segment Tree Query II
最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...
随机推荐
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
题目:http://codeforces.com/contest/397/problem/B 题意:给一个n ,求能不能在[l, r]的区间内的数字相加得到, 数字可多次重复.. 比赛的时候没有想出来 ...
- bzoj3551 3545
我直接来讲在线好了 这是一个很巧妙的方法,把边作为一个点 做一遍最小生成树,当加如一条边时,我们把这条边两点x,y的并查集的根i,j的父亲都设为这条边代表的点k,由k向i,j连边 这样我们就构建出一棵 ...
- UVa 11489 (博弈) Integer Game
一个数字能被3整除就等价于这个数的各个数字之和被3整除. 所以一开始的时候先要拿一个能使剩下的数字是3的倍数的数. 然后就一直拿0.3.6.9直到某人不能再拿为止. #include <cstd ...
- H.264码流结构解析
from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...
- delphi实现ado的高级功能
ADO是Microsoft存取通用数据源的标准引擎.ADO通过封装OLE DB而能够存取不同类型的数据,让应用程序能很方便地通过统一的接口处理各种数据库.ADO由一组COM对象组成,每一个不同的原生A ...
- android moveTaskToback 应用退到后台,类似最小化
方法:public boolean moveTaskToBack(boolean nonRoot) activity里有这个方法,参数说明如下: nonRoot=false→ 仅当activity为t ...
- 硬盘结构介绍--mbr及分区
硬盘刚买来使用时需要经过分区然后格式化才能够使用,硬盘经过分区后,分区软件便会写一个主引导扇区,这个扇区位于硬盘的 0 磁道 0 柱面第1扇区(即0区)(注意:该扇区为隐含扇区,0道0面的全部扇区均为 ...
- Oracle数据库表结构导出
1. 在PL/SQL中找到"工具--导出用户对象"菜单.点击运行.
- java中的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- virtualbox虚拟机中mysql远程连接登陆报2003错误的解决方法
最近在virtualbox中安装了Ubuntu 14,配置了一个mysql server,设置的桥接网络模式.在其他电脑连接的时候,总是报2003错误.开始以为是localhost没有置换为%,运行u ...