【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 k1 = 10 and k2 = 22, then your function should return [12, 20, 22].

    20
   /    8   22
 / 4   12

题解:

Solution 1 ()

class Solution {
public:
    /**
     * @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.
     */
    vector<int> searchRange(TreeNode* root, int k1, int k2) {
        vector<int> result;

        inOrder(result, root, k1, k2);

        return result;
    }

    void inOrder(vector<int> &result, TreeNode* root, int k1, int k2) {
        if (root == NULL) {
            return;
        }
        if (root->val > k1) {
            inOrder(result, root->left, k1, k2);
        }
        if (k1 <= root->val && root->val <= k2) {
            result.push_back(root->val);
        }
        if (root->val < k2) {
            inOrder(result, root->right, k1, k2);
        }
    }
};
时间: 2024-11-09 00:52:37

【Lintcode】011.Search Range in Binary Search Tree的相关文章

【Lintcode】087.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 a binary search tree after rem

【LeetCode】Convert Sorted List to Binary Search Tree 解题报告

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [解析] 分治:用快慢指针找到链表的中点,作为树的root,然后二分--中点之前的链表和中点之后的链表分别再构造二叉平衡树. /** * Definition for singly-linked list. * public class ListNode { * int

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

【转】STL之二分查找 (Binary search in STL)

Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第45条的一个总结,阐述了各种查找算法的异同以及使用他们的时机. 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range.带有判别式的如count_if,find_if或者binary_search的派别式版本,其

【leetcode】701. Insert into a Binary Search Tree

题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original B

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

【leetcode】Convert Sorted Array to Binary Search Tree (easy)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)

【树】Convert Sorted Array to Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路: 找到数组的中间数据作为根节点,小于中间数据的数组来构造作为左子树,大于中间数据的数组来构造右子树. /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.l