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 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的更多相关文章
- 【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 ...
- LintCode题解之Search Range in Binary Search Tree
1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...
- 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 ...
- 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 作者 ...
- 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 作者 ...
- 【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; ...
- [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. ...
- 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. ...
- 【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 ...
随机推荐
- the essence of the internet idea
Computer Systems A Programmer's Perspective Second Edition Of course, we are glossing over many diff ...
- Finally 与 return
网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...
- Best practice: escape, or encodeURI / encodeURIComponent
escape() Don't use it, as it has been deprecated since ECMAScript v3. encodeURI() Use encodeURI when ...
- php---文件上传分析
文件上传: 先抄一段:预定义变量$_FILES数组有5个内容: $_FILES['userfile']['name']——客户端机器文件的原名称 $_FILES['userfi ...
- gradlew常用命令
./gradlew -v 查看版本 ./gradlew clean 清理.下载依赖 ./gradlew build 构建 libgdx项目中的gradlew run: ./gradlew deskt ...
- ArcGIS API for Silverlight 绘制降雨路径动画
原文:ArcGIS API for Silverlight 绘制降雨路径动画 #region 降雨动画演示 2014-04-16 List<Graphic> graphics = new ...
- 高效的两段式循环缓冲区──BipBuffer
Simon Cooke,美国 (原作者) 北京理工大学 20981 陈罡(翻译) 写在前面的话: 循环缓冲区是一个非常常用的数据存储结构,已经被广泛地用于连续.流数据的存储和通信应用中.对于循环缓冲区 ...
- Appium-001-测试开发环境搭建(Android - Win7)
随着移动端 App 测试自动化的兴起,为更好的控制产品质量,越来越多的中大型公司开始了移动端的自动化测试.Appium 自动化测试技术也是我很早之前就想学习的一门技术,却一直没有比较空余的时间来学习( ...
- Java学习-006-三种数据库连接 MySQL、Oracle、sqlserver
此文主要讲述在初学 Java 时,常用的三种数据库 MySQL.Oracle.sqlserver 连接的源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源 ...
- php object转数组示例
原本是这样格式的数据: object(Thrift\Server\PageCards)#32 (3) { ["cards"]=> array(10) { [0]=> o ...