题目描述

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. 10月清北学堂培训 Day 1

    今天是杨溢鑫老师的讲授~ T1 1 题意: n * m 的地图,有 4 种不同的地形(包括空地),6 种不同的指令,求从起点及初始的状态开始根据指令行动的结果. 2 思路:(虽然分了数据范围但是实际上 ...

  2. python2和python3区别

    字符编码: py3中默认字符编码是unicode:py2中默认字符编码是 ASCII,如果文件中出现了中文,需要在顶部加入coding声明#coding:utf8 让用户输入:py3中直接使用inpu ...

  3. C++标准库分析总结(三)——<迭代器设计原则>

    本节主要总结迭代器的设计原则,以及iterstor traits的设计作用 1.迭代器遵循的原则 迭代器是算法和容器的桥梁,它是类模板的设计,迭代器必须有能力回答算法提出的问题才能去搭配该算法的使用 ...

  4. HDU 6041 I Curse Myself ——(仙人掌图,tarjan,转化)

    题解见这个博客:http://blog.csdn.net/ME495/article/details/76165039. 复杂度不太会算..这个经典问题的解法需要注意,维护队列里面只有k个元素即可.另 ...

  5. redisson spring boot starter 做分布式锁

    使用redisson做分布式锁 分布式锁 在java中单体应用中,我们如果想要保证一个接口或者服务.方法当下只有一个线程在运行,我们可以通过JDK提供的Lock.Semaphore.同步锁等多种方式实 ...

  6. 小程序tab切换代码

    <!--index.wxml--> <view class="container"> <view class="navtap" & ...

  7. formidable处理提交的表单或图片文件的简单介绍

    一般来说,客户端向服务端提交数据有GET和POST这两种方式,在之前的文章node.js当中的http模块与url模块的简单介绍当中我们可以知道通过req.url与url模块的配合处理可以快速得到客户 ...

  8. OpenResty之replace-filter-nginx-module

    原文: openresty/replace-filter-nginx-module 1. 概要 location /t { default_type text/html; echo abc; repl ...

  9. vue---vue2.x自定义plugin,给vue添加全局方法,原型上增加全局方法

    1. 自定义plugin.js export default{ install(Vue,options); { Vue.prototype.toStringTwo=(str)=>( ('0000 ...

  10. Flex 布局的最简单表单

    http://www.ruanyifeng.com/blog/2018/10/flexbox-form.html https://www.cnblogs.com/grt322/p/8531882.ht ...