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 ...
随机推荐
- RocketMq(一)初识消息中间件
1.对消息的理解 消息即为数据(data),数据有一定的规则.长度.大小. Java Message Service(Java消息服务)简称JMS,为Java 程序提供一种通用的方式,来创建.发送.接 ...
- 全网最详细的HA集群的主节点之间的双active,双standby,active和standby之间切换的解决办法(图文详解)
不多说,直接上干货! 1. HA集群的主节点之间的双standby的解决办法: 全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解) 2. HA集群的 ...
- kmp模式串匹配
大二的时候百度看不懂,现在大三下学期了百度也看不懂.(实在是不能理解这个next数组究竟是怎么样工作的,为什么会得出那样的结果.)如果有好心人知道的话希望可以联系我告诉我(邮箱:4609019410@ ...
- 利用Django构建web应用及其部署
注:很久之前就有了学习Django的想法,最近终于有机会做了一次尝试.由于Django的详细教程很多,我在这里就不再详述了,只是将整个开发流程以及自己在学习Django中的一些思考记录在此. Syst ...
- tomcat 调优-生产环境必备
目录 1. tomcat 启动慢 1.1 tomcat 获取随机值阻塞 1.2 tomcat 需要部署的web应用程序太多 1.3 tomcat启动内存不足 2 Connector 调优 2.2 Co ...
- idea编辑器无法识别jdk
File-->Invalidate Caches / Restart...-->Invalidate and Restart 然后就可以了
- 生成对抗网络(GAN)
基本思想 GAN全称生成对抗网络,是生成模型的一种,而他的训练则是处于一种对抗博弈状态中的. 譬如:我要升职加薪,你领导力还不行,我现在领导力有了要升职加薪,你执行力还不行,我现在执行力有了要升职加薪 ...
- Spring-Task思维导图
最近在搞一个定时任务的相关东西,为了方便记忆,这里将知识点总结成一个思维导图.后续也会通过思维导图的方式发布博客.
- UVA 1605 Building for UN(思维)
题目链接: https://cn.vjudge.net/problem/UVA-1605#author=0 /* 问题 设计一个包含若干层的联合国大厦,其中每一层都是等大的网格,每个格子分配给一个国家 ...
- JavaScript之String总汇
一.常用属性 ·length:返回字符串中字符长度 let str = 'asd '; str.length = 1;//无法手动修改,只读 console.log(str.length);//4 二 ...