题目:

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

  1. 20
  2. / \
  3. 8 22
  4. / \
  5. 4 12

题解:

Solution 1 ()

  1. class Solution {
  2. public:
  3. /**
  4. * @param root: The root of the binary search tree.
  5. * @param k1 and k2: range k1 to k2.
  6. * @return: Return all keys that k1<=key<=k2 in ascending order.
  7. */
  8. vector<int> searchRange(TreeNode* root, int k1, int k2) {
  9. vector<int> result;
  10.  
  11. inOrder(result, root, k1, k2);
  12.  
  13. return result;
  14. }
  15.  
  16. void inOrder(vector<int> &result, TreeNode* root, int k1, int k2) {
  17. if (root == NULL) {
  18. return;
  19. }
  20. if (root->val > k1) {
  21. inOrder(result, root->left, k1, k2);
  22. }
  23. if (k1 <= root->val && root->val <= k2) {
  24. result.push_back(root->val);
  25. }
  26. if (root->val < k2) {
  27. inOrder(result, root->right, k1, k2);
  28. }
  29. }
  30. };

【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. Netty(三):线程模型

    Netty中支持单线程模型,多线程模型,主从多线程模型. 1 单线程模型 在ServerBootstrap调用方法group的时候,传递的参数是同一个线程组,且在构造线程组的时候,构造参数为1,这种开 ...

  2. aar格式

    aar包是Android Library Project的二进制公布包. 文件的扩展名是aar,并且maven包类型也应该是aar. 只是这文件本身就是一个简单的zip文件.里面有例如以下的内容: / ...

  3. Chrome自带恐龙小游戏的源码研究(完)

    在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...

  4. Redis闲谈(1):构建知识图谱

    场景:Redis面试 (图片来源于网络) 面试官: 我看到你的简历上说你熟练使用Redis,那么你讲一下Redis是干嘛用的? 小明: (心中窃喜,Redis不就是缓存吗?)Redis主要用作缓存,通 ...

  5. 详解Linux三剑客之awk

    第一篇 awk简介与表达式实例 一种名字怪异的语言 模式扫描和处理,处理数据和生成报告. awk不仅仅是linux系统中的一个命令,而且是一种编程语言:它可以用来处理数据和生成报告(excel):处理 ...

  6. ASP.NET数据库连接字符串的加密与解密

    ASP.NET web.config中,数据库连接字符串的加密与解密. 虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容 加密: ...

  7. 未加载Microsoft.SqlServer.management.sdk.sfc version......

    这个问题卡了我好久,于是决定记录下来,我这里缺失的是Microsoft.SqlServer.management.sdk.sfc version 12.0.0,当然你也可能后面是11开头的, 这个是由 ...

  8. C#里类的get和set方法编写和调用

    using System; class Date { int day; int month; int year; public int Day{ get { return day; } set { d ...

  9. java 常用设计模式(转载)

    http://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html 设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得 ...

  10. ABAP 弹出框 函数

    POPUP_GET_VALUES_USER_HELP 是一个和用户交互信息的函数,用户能够填写信息,并且我们还能够依据实际的需求对弹出框进行F1 F4 以及用户的需求进行增强.具体的实现能够參考系统标 ...