LC 173. 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.
Example:

BSTIterator iterator = new BSTIterator(root);
iterator.next(); // return 3
iterator.next(); // return 7
iterator.hasNext(); // return true
iterator.next(); // return 9
iterator.hasNext(); // return true
iterator.next(); // return 15
iterator.hasNext(); // return true
iterator.next(); // return 20
iterator.hasNext(); // return false
参考答案
class BSTIterator {
private:
stack<TreeNode*> st;
public:
BSTIterator(TreeNode* root) {
foo(root);
}
/** @return the next smallest number */
int next() {
TreeNode* temp = st.top();
st.pop();
foo(temp->right);
return temp->val;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !st.empty();
}
void foo(TreeNode* root){
for(;root!=NULL;st.push(root),root=root->left);
}
};
答案解析
建立一个新的stack,其中存储包括自身的左孩子,这意味着整条树都存在里面,而最上边的永远是树的最左孩子。
next函数,就是把最上面的弹出来,将该点的右孩子(以及它的所有左孩子)给存了,从而保证最小的在第一个。
LC 173. Binary Search Tree Iterator的更多相关文章
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode 173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Java for LeetCode 173 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 173. Binary Search Tree Iterator
题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 173. Binary Search Tree Iterator -- 迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 理解 java 使用 异或 交换两数
网上看了一些使用异或交换两数,不是很好理解.现在写一下自己的理解. 首先是 异或原则,对于任意 x: x ^ x == 0; x ^ 0 == x; 思路: 根据原则,可以得到两个公式: 求a: ...
- linux 编写定时任务,查询服务是否挂掉
shell 脚本 #!/bin/bash a=`netstat -unltp|grep fdfs|wc -l` echo "$a" if [ "$a" -ne ...
- ros资料记录,详细阅读
ROS源码分析--子话题-catkin:https://blog.csdn.net/sukha/article/details/52460492 ROS源码分析:https://blog.csdn.n ...
- IDEA的版本控制
参考:https://blog.csdn.net/qq_35246620/article/details/70792861 1.从远程仓库下载项目 2.提交项目到远程仓库
- Chrome和火狐插件让数以百万计用户隐私数据泄露
https://tech.163.com/19/0721/12/EKK1PRAU00097U7R.html 网易科技讯7月21日消息,据国外媒体报道,流行浏览器诸如广告拦截等扩展功能,已经遭利 ...
- pom.xml中properties作用
可以在properties下声明相应的版本信息,然后在dependency下引用的时候用${spring-version}就可以引入该版本jar包了 <properties> <sp ...
- Java工作笔记:工作中使用JNA调用C++库的一些细节(转载)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/zjutzmh/article/detai ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_15-认证接口开发-Redis配置
4.2 Redis配置 4.2.1 安装Redis 1.安装Redis服务 下载Windows版本的redis:https://github.com/MicrosoftArchive/redis/ta ...
- myeclipse启动的过程中没提示就自动退出,闪退的有效解决方法
今天遇到一个问题,已经打开myeclipse的电脑因为非正常关机后myeclipse打不开了,进度条进到十分之一就闪退,什么提示都没有的解决方案如下: 1.打开myeclipse工作空间(存放项目 ...
- oracle数据库死锁原因及分析
定义: 当两个用户希望持有对方的资源时就会发生死锁. 即两个用户互相等待对方释放资源时,oracle认定为产生了死锁,在这种情况下,将以牺牲一个用户作为代价,另一个用户继续执行,牺牲的用户的事务将回滚 ...