173. Binary Search Tree Iterator

原创 2018年04月17日 01:57:50

问题描述

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


思路分析

实现一个二叉搜索树的迭代器,能够在O(1)的时间内返回下一个最小的数,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 {
    stack<TreeNode *> stk;
public:
    BSTIterator(TreeNode *root) {
        pushAll(root);
    }

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

    /** @return the next smallest number */
    int next() {
        TreeNode *temp = stk.top();
        stk.pop();
        pushAll(temp->right);
        return temp->val;
    }
private:
    void pushAll(TreeNode * node){
        while(node){
            stk.push(node);
            node = node->left;
        }
    }
};

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = BSTIterator(root);
 * while (i.hasNext()) cout << i.next();
 */

时间复杂度:O(1)
空间复杂度:O(n)


反思

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigFatSheep/article/details/79969009

leetcode-173:Binary Search Tree Iterator(Java)

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterato...
  • wen1158646729
  • wen1158646729
  • 2015-08-18 10:58:59
  • 1209

leetcode 173: Binary Search Tree Iterator

Binary Search Tree Iterator Total Accepted: 1141 Total Submissions: 3857 Implement an iterat...
  • xudli
  • xudli
  • 2015-01-02 12:44:56
  • 4142

[leetcode] 173. Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
  • TstsUgeg
  • TstsUgeg
  • 2016-05-20 20:58:34
  • 279

Leetcode 173 Binary Search Tree Iterator 二叉查找树迭代器

实现一个适用于二叉查找树的迭代器,该迭代器通过二叉查找树的根结点来实例化。调用next()返回二叉查找树中的最小的元素。next()和hasNext()运行的平均时间复杂度为O(1),空间复杂度为O(...
  • smile_watermelon
  • smile_watermelon
  • 2015-08-04 18:27:43
  • 1686

Leetcode: Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
  • doc_sgl
  • doc_sgl
  • 2015-01-03 22:13:09
  • 3680

[C++]LeetCode: 93 Binary Search Tree Iterator (经典题,非递归的中序遍历)

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with...
  • cinderella_niu
  • cinderella_niu
  • 2015-01-14 16:32:34
  • 1329

173. Binary Search Tree Iterator**

mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the roo...
  • alwaystry
  • alwaystry
  • 2016-12-24 14:35:54
  • 171

Binary Search Tree Iterator 二叉搜索树的遍历

Binary Search Tree Iterator   Implement an iterator over a binary search tree (BST). Your iterato...
  • u012605629
  • u012605629
  • 2015-05-28 22:19:41
  • 489

LeetCode(173) Binary Search Tree Iterator解题报告

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
  • Ssunsets
  • Ssunsets
  • 2015-12-15 22:43:38
  • 237

[Java]leetcode173 Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
  • fumier
  • fumier
  • 2015-04-10 08:52:17
  • 665
收藏助手
不良信息举报
您举报文章:173. Binary Search Tree Iterator
举报原因:
原因补充:

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