zoukankan      html  css  js  c++  java
  • 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.

    /**
     * Definition of TreeNode:
     * class TreeNode {
     * public:
     *     int val;
     *     TreeNode *left, *right;
     *     TreeNode(int val) {
     *         this->val = val;
     *         this->left = this->right = NULL;
     *     }
     * }
     */
    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) {
            // write your code here
            vector<int> seen;
            search(root, k1, k2, seen);
            return seen;
        }
        
        void search(TreeNode* root, int k1, int k2, vector<int>& seen) {
            if (k2 < k1 || root == NULL) {
                return;
            }
            if (k1 >= root->val) {
                // range all in right tree
                if (k1 == root->val) {
                    seen.push_back(k1);
                }
                search(root->right, k1, k2, seen);
            } else if (k2 <= root->val) {
                // range all in left tree
                search(root->left, k1, k2, seen);
                if (k2 == root->val) {
                    seen.push_back(k2);
                }
            } else {
                // normal
                search(root->left, k1, root->val, seen);
                seen.push_back(root->val);
                search(root->right, root->val, k2, seen);
            }
        }
    };
    
    

    把情况合并一下可以写成如下形式:

    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) {
            // write your code here
            vector<int> seen;
            search(root, k1, k2, seen);
            return seen;
        }
        
        void search(TreeNode* root, int k1, int k2, vector<int>& seen) {
            if (k2 < k1 || root == NULL) {
                return;
            }
            if (k1 < root->val) {
                search(root->left, k1, min(root->val, k2), seen);
            }
            if (k1 <= root->val && root->val <= k2) {
                seen.push_back(root->val);
            }
            if (k2 > root->val) {
                search(root->right, max(root->val, k1), k2, seen);
            }
        }
    };
    
  • 相关阅读:
    (原创)攻击方式学习之(4) 拒绝服务(DOS/DDOS/DRDOS)
    gdb调试程序
    找找看XSS漏洞!!!
    C++实现发送HTTP请求
    Python天天美味(32) python数据结构与算法之堆排序
    (原创)攻击方式学习之(3) 缓冲区溢出(Buffer Overflow)
    Python天天美味(30) python数据结构与算法之快速排序
    CoderZh首款Python联机对战游戏 NancyTetris1.0倾情发布(一)
    ASCII表查看
    wxpython学习笔记
  • 原文地址:https://www.cnblogs.com/lailailai/p/4832614.html
Copyright © 2011-2022 走看看