zoukankan      html  css  js  c++  java
  • 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

    Calling next() will return the next smallest number in the BST.

    Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

    解法一:暴力解法先不考虑空间复杂度

    中序遍历后装入队列,顺序输出。

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class BSTIterator {
    public:
        queue<int> minq;
        
        map<TreeNode*, bool> m;
        stack<TreeNode *> s;
        BSTIterator(TreeNode *root) {
            //inOrder traversal
            if(root != NULL)
            {
                s.push(root);
                m[root] = true;
                while(!s.empty())
                {
                    TreeNode* top = s.top();
                    if(top->left && m.find(top->left) == m.end())
                    {
                        s.push(top->left);
                        m[top->left] = true;
                        continue;
                    }
                    minq.push(top->val);
                    s.pop();
                    if(top->right && m.find(top->right) == m.end())
                    {
                        s.push(top->right);
                        m[top->right] = true;
                    }
                }
            }
        }
    
        /** @return whether we have a next smallest number */
        bool hasNext() {
            return !minq.empty();
        }
    
        /** @return the next smallest number */
        int next() {
            int front = minq.front();
            minq.pop();
            return front;
        }
    };
    
    /**
     * Your BSTIterator will be called like this:
     * BSTIterator i = BSTIterator(root);
     * while (i.hasNext()) cout << i.next();
     */

    解法二:空间复杂度O(h)的解法

    每次取出栈顶元素(即当前最小)后,查找下一个元素并压栈。

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class BSTIterator {
    public:
        stack<TreeNode*> stk;
        int nextmin;
        BSTIterator(TreeNode *root) {
            while(root)
            {
                stk.push(root);
                root = root->left;
            }
        }
    
        /** @return whether we have a next smallest number */
        bool hasNext() {
            if(!stk.empty())
            {
                TreeNode* top = stk.top();
                stk.pop();
                nextmin = top->val;
                TreeNode* cur = top->right;
                if(cur)
                {
                    stk.push(cur);
                    cur = cur->left;
                    while(cur)
                    {
                        stk.push(cur);
                        cur = cur->left;
                    }
                }
                return true;
            }
            else
                return false;
        }
    
        /** @return the next smallest number */
        int next() {
            return nextmin;
        }
    };
    
    /**
     * Your BSTIterator will be called like this:
     * BSTIterator i = BSTIterator(root);
     * while (i.hasNext()) cout << i.next();
     */

  • 相关阅读:
    POJ 2723 Get Luffy Out(2-SAT)
    ZOJ 3613 Wormhole Transport
    HDU 4085 Peach Blossom Spring
    NBUT 1221 Intermediary
    NBUT 1223 Friends number
    NBUT 1220 SPY
    NBUT 1218 You are my brother
    PAT 1131. Subway Map (30)
    ZSTU OJ 4273 玩具
    ZSTU OJ 4272 最佳淘汰算法
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4196016.html
Copyright © 2011-2022 走看看