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.

Have you met this question in a real interview?

Yes
Example

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

    20
/ \
8 22
/ \
4 12
Tags 
分析:
这题还是很简单的,只要根据root的值和k1, k2的值分三种情况讨论就可以了。
 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @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.
*/
public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
if (k1 <= k2) {
traverse(root, k1, k2, list);
}
Collections.sort(list);
return list;
} public void traverse(TreeNode node, int k1, int k2, ArrayList<Integer> list) {
if (node != null) {
if (node.val < k1) {
traverse(node.right, k1, k2, list);
} else if (node.val > k2) {
traverse(node.left, k1, k2, list);
} else {
list.add(node.val);
traverse(node.left, k1, k2, list);
traverse(node.right, k1, k2, list);
}
}
}
}

参考请注明出处:cnblogs.com/beiyeqingteng/

Search Range in Binary Search Tree的更多相关文章

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

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

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

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

  4. 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 作者 ...

  5. 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 作者 ...

  6. 【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; ...

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

  8. [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  9. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

随机推荐

  1. Luncene 学习入门

    Lucene是apache组织的一个用java实现全文搜索引擎的开源项目. 其功能非常的强大,api也很简单.总得来说用Lucene来进行建立 和搜索和操作数据库是差不多的(有点像),Document ...

  2. 从svn服务器自动同步到另一台服务器

    需求场景 A commit B post-commit C (workstation) --------------> (svn server) ---------------------> ...

  3. RequestMethod 相关

    Http协议的Delete和Put方法是做什么的?怎么用? RequestMethod 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一般来说,Web服务器默认的只支持Pos ...

  4. on the way to Peking University

    明天就要去北京参加北大夏令营了,希望这次能有所斩获! on the way to Peking University

  5. Java线程之间通信

    用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...

  6. Andriod Dialog 加载框 自定义,公用

    怎门样,看图吧,我感觉还不错,很好,在让美工给你陪陪色,就完美了 代码呢放csnd 一份,收1分,百度网盘一份免费.我csdn 没分了,资助我下,谢谢了. http://download.csdn.n ...

  7. [Angularjs]视图和路由(一)

    写在前面 对单页应用来讲,视图和路由的作用可以从一个视图跳转到另外一个视图,可以合理管理用户在使用过程中看到的界面. 将视图分解成布局和模版视图,并且根据用户当前访问的URL来展示对应的视图,将会是一 ...

  8. Egret命令行小结

    1. build 构建指定项目,编译指定项目的TypeScript文件2. create 创建新项目3. create_app 从h5游戏生成app4. create_mainfest 在工程目录下生 ...

  9. WIN 2003服务器终极安全及问题解决方案

    一.硬盘分区与操 作系统的安装硬盘分区 总的来讲在硬盘分区上面没什么值得深入剖析的地方,无非就是一个在分区前做好规划知道要去放些什么东西, 如果实在不知 道.那就只一个硬盘只分一个区,分区要一次性完成 ...

  10. C#设计模式(3):抽象工厂模式(Abstract Factory)(转载)

    概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...