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 ...
随机推荐
- 21335592 ROWS
CREATE TABLE w_big SELECT * FROM ( SEELCT * FROM w_tab UNION ALL SELECT * FROM w_tab_copy_modify ) ...
- C++ 栈和队列
使用标准库的栈和队列时,先包含相关的头文件 #include<stack> #include<queue> 定义栈如下: stack<int> stk; 定义队列如 ...
- linux下线程调用sleep,进程挂起
http://blog.csdn.net/horstlinux/article/details/7911457 http://blog.csdn.net/eroswang/article/detail ...
- Strom简介,以及安装,和官方案例测试
一:简介 1.strom的两种形式 2.strom的特性 3.使用场景 4.集群架构 5.集群架构进程 6.组件 Nimbus 7.从节点Supervisor 8.组件worker 9.组件Execu ...
- 利用快速排序原理找出数组中前n大的数
#include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...
- zepto源码--extend--学习笔记
对象继承函数: $.extend(){},默认传递一个参数,需要继承的对象目标.函数展示: 最终实现的过程,需要调用工具函数extend,首先分析extend函数. 默认传递三个参数,继承的目标对象- ...
- 流媒体学习二-------SIP协议学习(基本场景分析 )
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.SIP业务基本知识 1.1 业务介绍 会话初始协议(Session Initiation Protocol) ...
- Jquery调用webService的四种方法
1.编写4种WebService方法 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Conf ...
- Suricata配置文件说明
本系列文章是Suricata官方文档的翻译加上自己对其的理解,部分图片也是来自那篇文章,当然由于初学,很多方面的理解不够透彻,随着深入后面会对本文进行一定的修正和完善. Suricata使用Yaml作 ...
- std::map的clear()没有用?
昨天晚上,我徒弟跑过来讲,他的程序的内存占用居高不下,愿意是std::map的clear()没有效果.于是我让他用erase(begin,end); 试试也不行. 代码如下: void release ...