题目:

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. python3.x中xml.etree.ElementTree解析xml举例

    1.新建xml import xml.etree.ElementTree as ETa=ET.Element('elem')c=ET.SubElement(a,'child1')c.text=&quo ...

  2. Node.js下载及安装

    Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用. Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非常适 ...

  3. Ubuntu/CentOS下源码编译安装Php 5.6基本参数

    先确认安装libxml2 apt-get install libxml2 libxml2-dev或者yum install libxml2 libxml2-dev ./configure --pref ...

  4. 跨平台.NET Core--微软开源方向

     跨平台.NET Core--微软开源方向 微软宣布.net开源已经有一段时间了,新的跨平台的.net框架叫.NET Core. 当前支持Windows/Linux/OSX/Docker.官网:h ...

  5. EntityFramework走马观花之CRUD(下)

    我在Entity Framework系列文章的CRUD上篇中介绍了EF的数据查询,中篇谈到了EF的数据更新,下篇则聊聊EF实现CRUD的内部原理. 跟踪实体对象状态 在CRUD上篇和中篇谈到,为了实现 ...

  6. Ajax学习笔记(2)--load()方法

    <head runat="server"> <title></title> <script src="http://localh ...

  7. iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

    废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...

  8. Webpack探索【8】--- 模块热替换详解

    本文主要讲模块热替换相关内容.

  9. MethodDispatcher—Cherrypy对REST的支持

    前言 CherryPy是Python的一个Web框架,通过MethodDispatcher内建了对REST的支持,而且使用非常方便. 示例 首先,我们需要有一个符合REST风格的资源(Resource ...

  10. smartforms 条码打印

    转自:http://blog.csdn.net/zhongguomao/article/details/6759642 SAP 条码打印有 新旧 两种技术方法:1.传统的(旧):将数据发送到打印机上, ...