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.

Example
For example, if k1 = 10 and k2 = 22, then your function should print 12, 20 and 22. 20 / \ 8 22 / \ 4 12

我的做法是inorder traversal的变形,判断是否向左边递归的时候加上判断是否:root.val > k1, 如果否,则不需要继续向左递归;右子树的处理方法类似

第一次做法,把result数组作为return type,不好,消耗额外空间

 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) {
ArrayList<Integer> res = searchRangeRecur(root,k1,k2);
return res;
} public ArrayList<Integer> searchRangeRecur(TreeNode cur, int k1, int k2){
ArrayList<Integer> res = new ArrayList<Integer>();
if (cur==null) return res;
if (k1>k2) return res; ArrayList<Integer> left = searchRangeRecur(cur.left,k1,Math.min(cur.val-1,k2));
ArrayList<Integer> right = searchRangeRecur(cur.right,Math.max(cur.val+1,k1),k2); res.addAll(left);
if (cur.val>=k1 && cur.val<=k2) res.add(cur.val);
res.addAll(right); return res;
} }

第二遍做法:

 /**
* 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> res = new ArrayList<Integer>();
if (root == null || (k1 > k2)) return res;
helper(root, k1, k2, res);
return res;
} public void helper(TreeNode root, int k1, int k2, ArrayList<Integer> res) {
if (root == null) return;
helper(root.left, k1, k2, res);
if (k1 <= root.val && root.val <= k2) res.add(root.val);
helper(root.right, k1, k2, res);
}
}

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

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

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

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

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

  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. Lintcode: Remove Node in Binary Search Tree

    iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...

  9. 【Lintcode】095.Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

随机推荐

  1. Jsp入门学习笔记

    #Jsp入门 一.JSP基础语法 1.JSP指令: page inlcude taglib 2.JSP注释: a.html注释: <!-- abcdefghijklmn --> b.jsp ...

  2. <?php set_time_limit(10);

    <?php include('w_fun.php'); set_time_limit(10); Fatal error: Maximum execution time of 10 seconds ...

  3. Greedy_algorithm

    https://en.wikipedia.org/wiki/Greedy_algorithm

  4. android 设计工具栏

    设计工具栏Action Bar(订制工具栏类型) 工具栏给用户提供了一种熟悉和可预测的方式来执行某种动作和操纵应用程序,但是这并不意味着它就需要和其他的应用程序看起来一样的.如果想设计工具栏以使得它能 ...

  5. Android 学习

    入门: Android疯狂讲义 http://hukai.me/android-training-course-in-chinese/index.html http://segmentfault.co ...

  6. nrf51822-主从通信分析2

    解决第三个问题:如何使能从机上的特征值的 notify功能,使其能通过notify方式发送数据   使能从机的notify功能是通过写0x0001到从机的那个具有notify功能的特征值的CCCD描述 ...

  7. 我的第一个chrome扩展(2)——基本知识

    1.manifest介绍界面:json格式 json:JavaScript Object Notation 包括两种结构: key:value对:{{"A1":"valu ...

  8. 一种swift编码风格指南(供参考,by linkedin)

    http://www.cocoachina.com/swift/20160701/16894.html

  9. UDF

    一:UDF 1.自定义UDF 二:UDAF 2.UDAF 3.介绍AbstractGenericUDAFResolver 4.介绍GenericUDAFEvaluator 5.程序 package o ...

  10. Android笔记:Select at least one project解决办法

    导入项目的时候发现无法导入,最上方提示“Select at least one projec” 百度了一下,原来是和上一个项目名称相同了,直接把重名项目重命名后再导入即可.