题目:

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.

If k1 = 10 and k2 = 22, then your function should return [12, 20, 22].

    20
/ \
8 22
/ \
4 12

题解:

Solution 1 ()

class Solution {
public:
/**
* @param root: The root of the binary search tree.
* @param k1 and k2: range k1 to k2.
* @return: Return all keys that k1<=key<=k2 in ascending order.
*/
vector<int> searchRange(TreeNode* root, int k1, int k2) {
vector<int> result; inOrder(result, root, k1, k2); return result;
} void inOrder(vector<int> &result, TreeNode* root, int k1, int k2) {
if (root == NULL) {
return;
}
if (root->val > k1) {
inOrder(result, root->left, k1, k2);
}
if (k1 <= root->val && root->val <= k2) {
result.push_back(root->val);
}
if (root->val < k2) {
inOrder(result, root->right, k1, k2);
}
}
};

【Lintcode】011.Search Range in Binary Search Tree的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  4. 【leetcode】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  5. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  6. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

  7. 【PAT】1043 Is It a Binary Search Tree(25 分)

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  8. 【leetcode】701. Insert into a Binary Search Tree

    题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...

  9. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. ie6中利用jquery居中

    1.利用jquery居中代码 <script type="text/javascript"> $hwidth=parseInt($(window).width()); ...

  2. golang 内存池

    一般来说,内存池都是采用预分配的方式,分为固定大小的和非固定大小块,固定大小的内存效率高,非固定大小灵活.同时,分为单线程和多线程版的,单线程不需要考虑并发问题. 一般内存池的实现思想:分配一块比较大 ...

  3. 查看SELinux状态并关闭SELinux

    SELinux(Security-Enhanced Linux)是Linux上最杰出的新安全子系统.在linux内核级别上提供了一个灵活的强制访问控制系统(MAC),这个强制访问控制系统是建立在自由访 ...

  4. spring boot json 首字母大小写问题解决方案

     spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...

  5. 整合Settings.bundle显示版本信息

    本文转载至 http://www.cocoachina.com/ios/20141103/10112.html iOS开发XCode版本管理Debug开发Tips   现在你有一个App,你同事的iP ...

  6. python 基础 5.0 python类一般形式

    一. 类的一般形式 创建类我们一般使用class 关键字来创建一个类,class 后面跟类型名字,可以自定义,最后以冒号结尾,如下所示:   #/usr/bin/python #coding=utf- ...

  7. tp框架知识 之(链接数据库和操作数据内容)

    框架有时会用到数据库的内容,在"ThinkPhp框架知识"的那篇随笔中提到过,现在这篇随笔详细的描述下. 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写con ...

  8. Centos7升级python版本

    升级Python 我的安装目录是/home/python 下载 ` cd /home/python wget https://www.python.org/ftp/python/3.5.0/Pytho ...

  9. 搭建SVN服务器详细教程

    搭建SVN服务器详细教程 本教程会从最基本的下载安装到上传代码,下载代码这条线来详细讲述如何完成SVN服务器的搭建 下载并安装VisualSVN server 下载并安装TortoiseSVN 导入项 ...

  10. toitorsegit and toitorstsvn文件夹icon冲突不显示

    Go to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer Add a new string value & ...