Search Range in Binary Search Tree

Question

Problem Statement

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].

  1. 20
  2. / \
  3. 8 22
  4. / \
  5. 4 12

题解 - 中序遍历

中等偏易难度题,本题涉及到二叉查找树的按序输出,应马上联想到二叉树的中序遍历,对于二叉查找树而言,使用中序遍历即可得到有序元素。对每次访问的元素加以判断即可得最后结果,由于 OJ 上给的模板不适合递归处理,新建一个私有方法即可。

C++

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15. /**
  16. * @param root: The root of the binary search tree.
  17. * @param k1 and k2: range k1 to k2.
  18. * @return: Return all keys that k1<=key<=k2 in ascending order.
  19. */
  20. vector<int> searchRange(TreeNode* root, int k1, int k2) {
  21. vector<int> result;
  22. inorder_dfs(result, root, k1, k2);
  23. return result;
  24. }
  25. private:
  26. void inorder_dfs(vector<int> &ret, TreeNode *root, int k1, int k2) {
  27. if (NULL == root) {
  28. return;
  29. }
  30. inorder_dfs(ret, root->left, k1, k2);
  31. if ((root->val >= k1) && (root->val <= k2)) {
  32. ret.push_back(root->val);
  33. }
  34. inorder_dfs(ret, root->right, k1, k2);
  35. }
  36. };

Java

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param root: The root of the binary search tree.
  15. * @param k1 and k2: range k1 to k2.
  16. * @return: Return all keys that k1<=key<=k2 in ascending order.
  17. */
  18. public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
  19. ArrayList<Integer> result = new ArrayList<Integer>();
  20. helper(root, k1, k2, result);
  21. return result;
  22. }
  23. private void helper(TreeNode root, int k1, int k2, ArrayList<Integer> result) {
  24. if (root == null) return;
  25. // in-order binary tree iteration
  26. helper(root.left, k1, k2, result);
  27. if (k1 <= root.val && root.val <= k2) {
  28. result.add(root.val);
  29. }
  30. helper(root.right, k1, k2, result);
  31. }
  32. }

源码分析

以上为题解思路的简易实现,可以优化的地方为「剪枝过程」的处理——不递归遍历不可能有解的节点。优化后的inorder_dfs如下:

  1. void inorder_dfs(vector<int> &ret, TreeNode *root, int k1, int k2) {
  2. if (NULL == root) {
  3. return;
  4. }
  5. if ((NULL != root->left) && (root->val > k1)) {
  6. inorder_dfs(ret, root->left, k1, k2);
  7. } // cut-off for left sub tree
  8. if ((root->val >= k1) && (root->val <= k2)) {
  9. ret.push_back(root->val);
  10. } // add valid value
  11. if ((NULL != root->right) && (root->val < k2)) {
  12. inorder_dfs(ret, root->right, k1, k2);
  13. } // cut-off for right sub tree
  14. }

Warning 「剪枝」的判断条件容易出错,应将当前节点的值与k1k2进行比较而不是其左子节点或右子节点的值。