LeetCode173——Binary Search Tree Iterator

终于进入了中等难度的题目了。貌似leetcode不停地加题量,要做完正的是不那么容易啊。新年开工,小伙伴们都在晒红包,深圳这边 上班第一天一般都有发红包,不知道其他地方的是不是也这样。

到目前为止,做了40题了,不得不感慨,算法有待提高。大学真是白过了,人生那么美好的四年白过了,这是多么的悲哀。 现在想想,一个人要想在大学过得有意义,对以后的人生打下坚实的基础,那么最迟在大学一年之后,一定要有较广的见识,对社会有充分 的认识。总之,足够的广阔视野,才能让你知道当前的路怎么走。现实呢,大学专业往往都是父母帮填的,学生自己对专业是一无所知, 以至于进入大学后,不知道从何学起,如何学习,于是陷入漫长的迷茫期。当看清前路时,发现毕业来临了。

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.

难度系数:

中等

实现

class BSTIterator {
public:
     BSTIterator(TreeNode *root) {
        pos = 0;
        vector<TreeNode*> stack;
        if (root) {
            stack.push_back(root);
        }
        bool down = true;
        while (!stack.empty()) {
            TreeNode* node = stack.back();
            if (node->left != NULL && down) {
                stack.push_back(node->left);
            } else {
                down = false;
                nv.push_back(node->val);
                stack.pop_back();
                if (node->right) {
                    stack.push_back(node->right);
                    down = true;
                }
            }
        }
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return pos < nv.size();
    }

    /** @return the next smallest number */
    int next() {
        return nv[pos++];
    }
private:
    int pos;
    vector<int> nv;
};
时间: 2024-10-11 11:52:53

LeetCode173——Binary Search Tree Iterator的相关文章

[Java]leetcode173 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

[LeetCode] Binary Search Tree Iterator

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

LeetCode: Binary Search Tree Iterator 解题报告

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

[LintCode] Binary Search Tree Iterator

Binary Search Tree Iterator Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-order traversal) next() and hasNext() queries run in O(1) time inaverage. Example For the following

【LeetCode】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

Binary Search Tree Iterator

QUESTION 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

173. Binary Search Tree Iterator - Unsolved

https://leetcode.com/problems/binary-search-tree-iterator/#/description 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 B

[email&#160;protected] [173] Binary Search Tree Iterator (InOrder traversal)

https://leetcode.com/problems/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: nex

【leetcode】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