题目链接: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.

这道题的要求是实现二叉查找树(BST)的迭代器,其中迭代器根据BST的根节点进行初始化,调用next()函数将返回BST的下一个最小的数值。要求next()和hasNext()为O(1)时间复杂度,O(h)空间复杂度(h为BST高度)。

可以注意到,这就是一步一步地进行先序遍历,因此可以采用栈进行存储。

用栈存储每层未遍历的左子节点,然后hasNext()就是判断栈是否为空。每次next(),取出栈顶元素t,即为下一最小元素,同时还需要对t的右子数开始的每层左子节点压入栈。

时间复杂度:O(logn)

空间复杂度:O(logn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class BSTIterator
{
    stack<TreeNode *> stp;
public:
    BSTIterator(TreeNode *root)
    {
        pushLeft(root);
    }

    /** @return whether we have a next smallest number */
    bool hasNext()
    {
        return !stp.empty();
    }

    /** @return the next smallest number */
    int next()
    {
        TreeNode *temp = stp.top();
        stp.pop();
        pushLeft(temp -> right);
        return temp -> val;
    }
private:
    void pushLeft(TreeNode *root)
    {
        for(TreeNode *p = root; p != NULL; p = p -> left)
            stp.push(p);
    }
};