binary-search-tree-iterator
来源:互联网 发布:我知你忌讳是什么意思 编辑:程序博客网 时间:2024/06/07 23:57
https://leetcode.com/problems/binary-search-tree-iterator/
利用非递归 中序遍历的思想
class BSTIterator {private: stack<TreeNode*> s;public: BSTIterator(TreeNode *root) { while (!s.empty()) s.pop(); while (root) { s.push(root); root = root->left; } } /** @return whether we have a next smallest number */ bool hasNext() { return !s.empty(); } /** @return the next smallest number */ int next() { TreeNode *temp; int minvalue; temp = s.top(); s.pop(); minvalue = temp->val; temp = temp->right; while (temp) { s.push(temp); temp = temp->left; } return minvalue; }};
1 0
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- thinking in java 21_03
- bzoj2241 打地鼠 暴力&&二维差分
- CodeForces 609 B. The Best Gift(水~)
- GCD详解
- centos6修改YUM源方法
- binary-search-tree-iterator
- Android ListView 的基本用法
- R语言学习:数据结构6-缺失值
- 编写用户侧定时器
- 【quickSort】
- linux学习之c注释
- Cpp_拷贝构造与拷贝赋值
- 数据切分——Atlas读写分离Mysql集群的搭建
- CF 116A Tram