20150707 lintcode 总结 Search Range in Binary Search Tree

原创 2015年07月07日 12:02:42

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

/**
 * 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.
     */
    private ArrayList<Integer> results = new ArrayList<Integer> ();
    public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
        // write your code here
        helper(root, k1, k2);
        return results;
    }
    
    private void helper(TreeNode root, int k1, int k2) {
        if (root == null) {
            return;
        }
        if (root.val > k1) {
            helper(root.left, k1, k2);
        }
        if (root.val >= k1 && root.val <= k2) {
            results.add(root.val);
        }
        if (root.val < k2) {
            helper(root.right, k1, k2);
        }

    }
}












; ;; ;;;;;;;;;;;;;';:::

相关文章推荐

lintcode search-range-in-binary-search-tree 二叉搜索树中搜索区间

问题描述lintcode笔记一开始没有想到怎样利用二叉搜索树的性质简化程序,强行中序遍历了树,如代码1。其实还是应该利用二叉搜索树的性质。中序遍历是一定的访问左孩子--访问当前节点--访问右孩子可以做...

20150708 lintcode 总结 Binary Search Tree Iterator

Binary Search Tree Iterator Design an iterator over a binary search tree with the following rul...

#11 Search Range in Binary Search Tree

题目描述: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. ...

LeetCode-501. Find Mode in Binary Search Tree(Java)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred...
  • NOadu
  • NOadu
  • 2017年07月19日 16:52
  • 162

501. Find Mode in Binary Search Tree\492. Construct the Rectangle\*486. Predict the Winner\输出一个level

Find Mode in Binary Search Tree 题目描述 代码实现501. Find Mode in Binary Search Tree题目描述Given a binary sear...

[Leetcode] 255. Verify Preorder Sequence in Binary Search Tree 解题报告

题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of ...

LeetCode 255. Verify Preorder Sequence in Binary Search Tree

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

[leetcode]: 501. Find Mode in Binary Search Tree

1.题目Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occu...

LeetCode 255. Verify Preorder Sequence in Binary Search Tree(检查二叉搜索树的前序遍历)

原题网址:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ Given an array ...
  • jmspan
  • jmspan
  • 2016年04月09日 04:44
  • 395

leetcode501. Find Mode in Binary Search Tree

501. Find Mode in Binary Search TreeGiven a binary search tree (BST) with duplicates, find all the m...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:20150707 lintcode 总结 Search Range in Binary Search Tree
举报原因:
原因补充:

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