LintCode题解之Search Range in Binary Search Tree
1、题目描述
2、问题分析
首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字。
3、代码
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/**
* @param root: param root: The root of the binary search tree
* @param k1: An integer
* @param k2: An integer
* @return: return: Return all keys that k1<=key<=k2 in ascending order
*/
vector<int> searchRange(TreeNode * root, int k1, int k2) {
// write your code here
vector<int> num ;
inorder(num, root); vector<int> res;
if( num.size() == )
return res;
int i = ;
int j = num.size() - ;
while( num[i] < k1) i++;
while( num[j] > k2) j--; for(int k= i; k <= j; k++){
res.push_back(num[k]);
} return res; } void inorder(vector<int>& nums , TreeNode *root){
if( root != NULL ){
inorder(nums, root->left);
nums.push_back(root->val);
inorder(nums,root->right);
}else {
return ;
} } };
LintCode题解之Search Range in Binary Search Tree的更多相关文章
- 【Lintcode】011.Search Range in Binary Search Tree
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...
- Lintcode: Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- 04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...
- 【Leetcode_easy】700. Search in a Binary Search Tree
problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...
- 【Lintcode】087.Remove Node in Binary Search Tree
题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...
- 475. Heaters (start binary search, appplication for binary search)
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
随机推荐
- 2014.10.5 再次学习LINUX
mesg 发送信息给root y n write/talk 写消息给 wall 给所有用户发送消息 ps -aux ps -elF pstree 命令行跳转:CTRL+a行首 CTRL+e行尾 CTR ...
- sftp命令不被识别
sftp命令不被识别 原因:C:\Windows\System32文件夹下面没有sftp可执行程序 解决方案:安装openssh,安装完成之后可发现在path系统变量的值中多了openssh的安装目录 ...
- Android之内存泄漏
开篇之前,我们要先理解:什么是内存泄漏.百度百科:内存泄漏(Memory Leak)是指程序中己动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等 ...
- Kafka 副本失效
Kafka源码注释中说明了一般有两种情况会导致副本失效: follower副本进程卡住,在一段时间内根本没有想leader副本发起同步请求,比如频繁的Full GC. follower副本进程同步过慢 ...
- docker(二)部署docker容器虚拟化平台
yum安装方法参考:https://www.cnblogs.com/yufeng218/p/8370670.html https://www.cnblogs.com/straycats/p/84112 ...
- 解析Resources.arsc
一.前言 对于APK里面的Resources.arsc文件大家应该都知道是干什么的(不知道的请看我的另一篇文章Android应用程序资源文件的编译和打包原理),它实际上就是App的资源索引表.下面我会 ...
- 使用netfilter_queue改包笔记
系统:centos 7 准备:安装libnetfilter_queue模块,可以yum安装,也可以网上下载rpm包安装 简介:使用iptables在NAT表上创建DNAT与SNAT规则,对数据包进行转 ...
- oracle 删除服务sc delete Oracle
oracle 删除服务sc delete OracleVssWriterorcl -----(oracle orcl vss writer service的服务名!)
- 用SVN进行团队开发协作生命周期详解
目录 前言 面向人群 背景 解决方案 团队开发生命周期 创建新项目 创建分支 切换分支 合并代码 正式版本发布 bug修复 结束语 前言 查找了SVN的相关知识无论是园子里还是百度都只有一些理论,而有 ...
- RESTful API入门
RESTful是一种设计风格,并不是一种标准. 简短的去概括的话,就是:1.URL 定位资源 资源,就是数据.比如newsfeed,friends,order等 2.用 HTTP 动词描述操作. ...