题目

二叉查找树中搜索区间

给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点。找到树中所有值在 k1 到 k2 范围内的节点。即打印所有x (k1 <= x <= k2) 其中 x 是二叉查找树的中的节点值。返回所有升序的节点值。

样例

如果有 k1 = 10 和 k2 = 22, 你的程序应该返回 [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> inorder = inorderTraveral(root);
ArrayList<Integer> result = new ArrayList<Integer>();
for(int i=0;i< inorder.size();i++){
int tmp = inorder.get(i);
if(k1<= tmp && tmp <= k2){
result.add(tmp);
}
}
return result;
}
public ArrayList<Integer> inorderTraveral(TreeNode root){
ArrayList<Integer> inorder = new ArrayList<Integer>();
if(root == null){
return inorder;
}
inorder.addAll(inorderTraveral(root.left));
inorder.add(root.val);
inorder.addAll(inorderTraveral(root.right));
return inorder;
}
}

Java Code

也可以直接中序遍历过程中,符合条件的节点值加入到ArrayList中去

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

Java Code

Python 实现

Python 中 append 是在list 后面添加元素

+= 是连接两个list

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
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.
"""
def searchRange(self, root, k1, k2):
# write your code here
return self.inorderTraveral(root,k1,k2) def inorderTraveral(self,root,k1,k2):
inorder = list()
if root == None:
return inorder
left = self.inorderTraveral(root.left,k1,k2)
if len(left)!=0:
inorder += left
if root.val>= k1 and root.val <= k2:
inorder.append(root.val)
right = self.inorderTraveral(root.right,k1,k2)
if len(right)!=0:
inorder += right
return inorder


lintcode: 二叉查找树中搜索区间的更多相关文章

  1. LintCode 11 二叉查找树的搜索区间

    题目链接:http://www.lintcode.com/zh-cn/problem/search-range-in-binary-search-tree/ 1.描述 给定两个值 k1 和 k2(k1 ...

  2. lintcode-11-二叉查找树中搜索区间

    二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= k2 ...

  3. lintcode: search for a range 搜索区间

    题目 搜索区间 给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置. 如果目标值不在数组中,则返回[-1, -1] 样例 给出[5, 7, 7, 8, 8, 10]和目 ...

  4. lintcode:在二叉查找树中插入节点

    题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样 ...

  5. LintCode-61.搜索区间

    搜索区间 给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置. 如果目标值不在数组中,则返回[-1, -1] 样例 给出[5, 7, 7, 8, 8, 10]和目标值t ...

  6. 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

    [033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Suppose a sort ...

  7. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. 在DOM中搜索元素

    方法 现代浏览器中使用XPath document.getElementById document/node.getElementsByTagName Limit search by parent e ...

  9. 在指定的div中搜索内容,并滚动显示到当前搜索到的内容处

    我想要的是页面中有个带滚动条的div对象,里面有很多内容,想要用js搜索到div中的某个字符串内容,然后将div的滚动条滚动到搜索到的内容处显示,自动定位.先是查找页面中的内容,然后将找到的内容创建t ...

随机推荐

  1. php 文件上传简单类---限制仅上传jpg文件

    php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...

  2. Python脚本控制的WebDriver 常用操作 <十五> 处理Navigation Bar

    下面将使用WebDriver来模拟操作:选择一个Navigation bar的选项 测试用例场景 Navigation Bar可以看作是简单的类似于tab的导航栏.一般来说导航栏都是ul+li.先定位 ...

  3. Microsoft server software support for Microsoft Azure virtual machines

    http://support.microsoft.com/kb/2721672/en-us  Article ID: 2721672 - Last Review: November 22, 2014 ...

  4. SQLserver中的xp_cmdshell

    shell是用户与操作系统对话的一个接口,通过shell告诉操作系统让系统执行我们的指令 xp_cmdshell在sqlserver中默认是关闭的存在安全隐患. --打开xp_cmdshell ;;R ...

  5. UnitTest

    using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...

  6. 短小强悍的JavaScript异步调用库

    对于博文 20行完成一个JavaScript模板引擎 的备受好评我感到很惊讶,并决定用此文章介绍使用我经常使用的另一个小巧实用的工具.我们知道,在浏览器中的 JavaScript 绝大部分的操作都是异 ...

  7. WWDC 2016: Rich Notifications in iOS 10

    Notifications have gotten more than a visual refresh in iOS 10. As part of the new UserNotifications ...

  8. Careercup - Google面试题 - 4857362737266688

    2014-05-04 00:10 题目链接 原题: Write a function return an integer that satisfies the following conditions ...

  9. adb 选择设备

    在adb中有多个设备时,可以先adb devices列举出设备,然后可以通过adb -s <设备名>  [其他参数] 对某个设备进行操作. 例如: adb -s 0123456789ABC ...

  10. P1676陶陶吃苹果 - vijos

    描述 curimit知道陶陶很喜欢吃苹果.于是curimit准备在陶陶生日的时候送给他一棵苹果树. curimit准备了一棵这样的苹果树作为生日礼物:这棵苹果树有n个节点,每个节点上有c[i]个苹果, ...