Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote the number of elements in the the array which value is between interval start and end. (The array may not fully filled by elements) Design a query method with three parameters root, start and end, find the number of elements in the in array's interval [start, end] by the given root of value SegmentTree. Have you met this question in a real interview? Yes
Example
For array [0, 2, 3], the corresponding value Segment Tree is: [0, 3, count=3]
/ \
[0,1,count=1] [2,3,count=2]
/ \ / \
[0,0,count=1] [1,1,count=0] [2,2,count=1], [3,3,count=1]
query(1, 1), return 0 query(1, 2), return 1 query(2, 3), return 2 query(0, 2), return 2 Note
It is much easier to understand this problem if you finished Segment Tree Buildand Segment Tree Query first.
注意23行
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, count;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int count) {
* this.start = start;
* this.end = end;
* this.count = count;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
*@param root, start, end: The root of segment tree and
* an segment / interval
*@return: The count number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
// write your code here
if (root == null || root.end < start || root.start > end) return 0;
if (root.start>=start && root.end<=end) return root.count;
int mid = (root.start + root.end)/2;
if (end <= mid) return query(root.left, start, end);
else if (start > mid) return query(root.right, start, end);
else return query(root.left, start, mid) + query(root.right, mid+1, end);
}
}
Lintcode: Segment Tree Query II的更多相关文章
- 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 Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- 247. Segment Tree Query II
最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- [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 Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 202. Segment Tree Query
最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...
随机推荐
- Programming In hardware Programming in software
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- Types of compression algorithms
http://www.html5rocks.com/en/tutorials/speed/img-compression/ Types of compression algorithms There ...
- SQLite(快速上手版)笔记
原文 1. SQL语法关键字 关键字 描述 Create Table 创建数据表 Alter Table 修改数据表 Drop Table 删除数据表 Create Index 创建索引 Drop I ...
- go liteIDE
go liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...
- C# DateTime类型和时间戳 互相转换
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- Qt调用dll中的功能函数
声明: 事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的.并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下: ...
- Moogoose操作之Schema实现增删查改
Schema不仅定义了文档结构和使用性能,可以为后面的Model和Entity提供公共的属性和方法. Schema.Model.Entity的关系: Schema : 可以定义字段类型,不具备数据库的 ...
- Tomcat执行流程
- 关于Xib 需要注意的地方
当你需要写一个繁琐的界面但是又是静态页面时,很可能会用到xib [特例:创建继承于UIView的文件时,不带自动生成xib的勾选项,需要开发人员手动new一个同名的xib文件,在该xib文件右侧的cl ...
- JQuery事件的链式写法
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...