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, 如果否,则不需要继续向左递归;右子树的处理方法类似

 1 public class Solution {
 2     /**
 3      * @param root: The root of the binary search tree.
 4      * @param k1 and k2: range k1 to k2.
 5      * @return: Return all keys that k1<=key<=k2 in ascending order.
 6      */
 7     public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
 8         ArrayList<Integer> res = searchRangeRecur(root,k1,k2);
 9         return res;
10     }
11
12     public ArrayList<Integer> searchRangeRecur(TreeNode cur, int k1, int k2){
13         ArrayList<Integer> res = new ArrayList<Integer>();
14         if (cur==null) return res;
15         if (k1>k2) return res;
16
17         ArrayList<Integer> left = searchRangeRecur(cur.left,k1,Math.min(cur.val-1,k2));
18         ArrayList<Integer> right = searchRangeRecur(cur.right,Math.max(cur.val+1,k1),k2);
19
20         res.addAll(left);
21         if (cur.val>=k1 && cur.val<=k2) res.add(cur.val);
22         res.addAll(right);
23
24         return res;
25     }
26
27 }
时间: 2024-08-08 05:37:52

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 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 k

Lintcode11 Search Range in Binary Search Tree solution 题解

[题目描述] 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. 给

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. Have you

PAT Search in a Binary Search Tree

Search in a Binary Search Tree To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For exa

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 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

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 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted

[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. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for e

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still