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. Could not open Hibernate Session for transaction;

    javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Co ...

  2. codeforces 720A:Closing ceremony

    Description The closing ceremony of Squanch Code Cup is held in the big hall with n × m seats, arran ...

  3. jQuery根据下拉列表的选择进行不同的操作

    需求:选择了某个下拉列表选项,进行不同的操作 代码部分: <!doctype html> <html> <head> <meta charset=" ...

  4. Laravel5.1 启动详解

    借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...

  5. POJ2676Sudoku(类似于八皇后)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16444   Accepted: 8035   Special ...

  6. git 提交时保存 帐号密码

    在相应的文件夹上右键 Tortiusegit->settings->gig 点击 第1个按钮 Edit local .git/config 在打开的文档里 url = http://xx. ...

  7. 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样)

    1.新建一个继承自UITableViewCell的子类  2. 在initWithStyle:方法中进行子控件的初始化 1> 将有可能显示的所有子控件都添加到contentView中 2> ...

  8. ubuntu12.04配置静态IP及设置DNS

    静态IP配置方法: 编辑/etc/network/interfaces,删掉内容,并输入以下几行(假设你的网卡是eth0) sudo gedit /etc/network/interfaces aut ...

  9. Java对文件及文件夹的操作

    public class FileOperater { // 验证字符串是否为正确路径名的正则表达式 private static String matches = "[A-Za-z]:\\ ...

  10. WPF 检测管理员权限

    // 检查是否是管理员身份 private static void CheckAdministrator() { WindowsIdentity wi = null; try { wi = Windo ...