binary-search-tree-iterator
2016-02-29 21:24
183 查看
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; } };
相关文章推荐
- java多线程系列01——多线程基础
- GCD详解
- poj1201Intervals——差分约束系统
- hdu1083二分图匹配
- Android ListView 的基本用法
- 探索工作流(八)--使用总结
- HibernateDaoSupport的使用
- ActiveSync不能连接开发板的问题
- BZOJ2879 [Noi2012]美食节
- Hibenate错误汇总:java.lang.NoClassDefFoundError:
- django比较相等或者不相等的模板语法ifequal / ifnotequal
- CodeForces 609 B. The Best Gift(水~)
- Spring进阶之路(4)-容器中Bean作用域与集合类型配置
- R语言学习:数据结构5-因子
- poj2699
- inline-block的特点
- QT for Windows CE 5.0/6.0
- 字符串的排列
- ubuntu 搭建Android开发环境
- lua 与 c 的相互调用