题目描述

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的更多相关文章

  1. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  2. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  3. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. leetcode 173. Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. 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 ...

  6. 173. Binary Search Tree Iterator

    题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...

  7. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  8. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. 173. Binary Search Tree Iterator -- 迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. Python基础之可接受任意数量参数的函数

    1. 可接受任意数量位置参数的函数 为了能让一个函数接受任意数量的位置参数,可以在参数部分使用“*”. def avg(first, *rest): return (first + sum(rest) ...

  2. [转载]virtual topology虚拟拓扑

    原文地址:topology虚拟拓扑">virtual topology虚拟拓扑作者:一丝尘埃 topology虚拟拓扑" title="[转载]virtual to ...

  3. linux的常见目录

    常见的目录和作用 目录名 目录名的作用 /bin/ 存放系统命令的目录,普通用户和root都使用,不过放在bin/命令下的单用户模式也可执行 /sbin/ 保存于系统环境相关的命令,只有root可以使 ...

  4. Go 语言入门(二)方法和接口

    写在前面 在学习 Go 语言之前,我自己是有一定的 Java 和 C++ 基础的,这篇文章主要是基于A tour of Go编写的,主要是希望记录一下自己的学习历程,加深自己的理解 Go 语言入门(二 ...

  5. 小福bbs-凡事预则立

    [小福bbs-凡事预则立] 1.冲刺的时间计划安排(冲刺时间为期七天,安排在2019-11-3--2019-11-14之间) 冲刺的时间 计划安排 2019.11.7 开会,安排具体工作 2019.1 ...

  6. 安装kvm安装虚拟机centos

    1 安装阿里云的镜像站 #将原来的镜像备份 cd /etc/yum.repos.d/ mkdir back mv CentOS-Base.repo ./back/ # 安装阿里的镜像 wget -O ...

  7. androidStudio: ERROR: Error occurred while communicating with CMake server.

    遇到此错误的原因是cmake服务器协议版本不匹配: 解决方案: 1:直接更新android studio看能否解决: 2:如果解决不了,那么将androidstudio,ndk ,cmake,grad ...

  8. keras输出预测值和真实值

    在使用keras搭建神经网络时,有时需要查看一下预测值和真是值的具体数值,然后可以进行一些其他的操作.这几天查阅了很多资料.好像没办法直接access到训练时的数据.所以我们可以通过回调函数,传入新的 ...

  9. Android:Recents和AMS中历史任务的区别

    1.1 任务和返回栈 - 实际数据模型  这个是指在调度体系里实际保存的TaskRecord实例,而ActivityRecord-TaskRecord-ActivityStack之间的关系建议看官方文 ...

  10. linuxan安装redis出现Newer version of jemalloc required错误

    linux安装redis执行make命令时出现问题 解决方法 make MALLOC=libc 如果要指定安装文件夹 使用命令:make PREFIX  = /usr/local/redis(文件夹路 ...