11. Search Range in Binary Search Tree

原创 2018年04月15日 13:54:12

11. Search Range in Binary Search Tree

Description

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

If k1 = 10 and k2 = 22, then your function should return [12, 20, 22].

    20
   /  \
  8   22
 / \
4   12

Solution

import java.util.*;
/**
 * 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 {
    List<Integer> list = new ArrayList<>();
    /**
     * @param root: param root: The root of the binary search tree
     * @param k1: An integer
     * @param k2: An integer
     * @return: return: Return all keys that k1<=key<=k2 in ascending order
     */
    public List<Integer> searchRange(TreeNode root, int k1, int k2) {
        // write your code here
        if(k1>k2) return list;
        if(root!=null){
            searchRange(root.left,k1,k2);
            if(root.val>=k1 && root.val<=k2) list.add(root.val);
            searchRange(root.right,k1,k2);
        }
        return list;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foradawn/article/details/79948791

thinkphp5微信开发实践教程

-
  • 1970年01月01日 08:00

lintcode:Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 Example For example, if k1 = 10 and k2 = 22, then your...
  • martin_liang
  • martin_liang
  • 2015-05-16 22:22:15
  • 488

[刷题]Search Range in Binary Search Tree

[LintCode]Search Range in Binary Search Tree VERSION I /** * Definition of TreeNode: * public cla...
  • willshine19
  • willshine19
  • 2015-03-30 19:15:22
  • 186

数据结构(Java语言)——BinarySearchTree简单实现

二叉树的一个重要应用是它们在查找中的使用。使二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有项的值都大于X中的项。注意,这意味着该树所有的元素都可以用某种一致的方式排序。 现在给出...
  • zhang_zp2014
  • zhang_zp2014
  • 2015-08-14 21:55:34
  • 1086

lintcode Binary Search

For a given sorted array (ascending order) and a target number, find the first index of this numbe...
  • susser43
  • susser43
  • 2015-08-03 18:09:35
  • 486

二叉搜索树(Binary Search Tree )的定义及分析

定义:     二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 每个结点都有一个作为搜索依据的关键码(key),所有结点的关键码互不相同。 左子树(如果非空)上所...
  • jsh13417
  • jsh13417
  • 2013-01-22 21:59:50
  • 3550

数据结构-BST(Binary Search Tree)

/****************************************** 数据结构: BST(Binary Search Tree),二叉查找树; 性质: 若结点的左子树不空,则左子树...
  • Jarily
  • Jarily
  • 2013-03-15 19:00:18
  • 7273

[LeetCode] 501. Find Mode in Binary Search Tree 解题报告

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurre...
  • a921122
  • a921122
  • 2017-04-28 05:41:56
  • 1390

20150707 lintcode 总结 Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 Example If k1 = 10 and k2 = 22, then your function sho...
  • u014748614
  • u014748614
  • 2015-07-07 12:02:42
  • 362
收藏助手
不良信息举报
您举报文章:11. Search Range in Binary Search Tree
举报原因:
原因补充:

(最多只允许输入30个字)