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.
If k1 = 10
and k2 = 22
, then your function should return[12, 20, 22]
.
20
/ \
8 22
/ \
4 12
/**
* 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的更多相关文章
- 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 ...
- 【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 ...
- 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. ...
- [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 ...
- [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 ...
随机推荐
- iOS开发小技巧--设置cell左右有空隙,设置分割线的新思路,重写setFrame:让别人在外界无法修改控件的大小
如图:需要自定义cell
- iOS边练边学--iOS中的(ARC下)单粒模式(GCD实现)
一.ARC中实现单粒模式 在.m 保留一个全局的static的实例 static id _名称; 重写allocWithZone:方法,在这里创建唯一的实例 提供一个类方法让外界访问唯一的实例 实现c ...
- JS高级设计第七章——复习知识点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SP*
1.PS1——默认提示符 root@tcx2250-14:/etc# echo $PS1\u@\h:\w\$ \u是用户名 \h是主机名 \w是当前目录的完整路径.请注意当你在主目录下的时候,如上面所 ...
- Day5_作业
ATM作业:1.额度15000或自定义2.实现购物商城,买东西加入购物车,调用信用卡接口结账3.可以提现,手续费5%4.每月22号出账单,每月10号为还款日,过期未还,按欠款总额万分之5每日计息5. ...
- 42.Android之ListView中ArrayAdapter简单学习
今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...
- input使用javascript限制输入带小数的数字
如题,网上找了很多都不太好实现.我的实现需求如下: 1.如果输入数字不带小数点那么自动加入两位小数,如:输入5,替换为5.00 2.输入5.,替换为5.00 3.输入5.1,替换为5.10 4.输入非 ...
- asp.net input怎么获取值
前台: <input type="hidden" name="content" value="content"> 后台: Req ...
- C#用UPnP穿透内网
参考了网上的一篇文章,由于时间长了,具体地址不知道了. 引入了一个DLL: Interop.NATUPNPLib.dll,实现穿透局域网,进行Socket通信. using System; using ...
- 用requests库实现登录遇到的问题
想登录zhihu,然后总是得到403 foribidden的错误,各种谷歌百度,得到结论说是输入错误或者是url错误,用fldder发现的确是url错了,post的地址是错误的 ==. 开始以为是#s ...