https://leetcode.com/problems/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.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public: queue<int> Q;
public:
BSTIterator(TreeNode *root) {
inOrder(root, Q);
} void inOrder(TreeNode* root, queue<int>& Q) {
if(root != NULL) {
inOrder(root->left, Q);
Q.push(root->val);
inOrder(root->right, Q);
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return !Q.empty();
} /** @return the next smallest number */
int next() {
int next_min = Q.front();
Q.pop();
return next_min;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)的更多相关文章

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

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

  2. leetcode 173. Binary Search Tree Iterator

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

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

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

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

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

  7. 【leetcode】Binary Search Tree Iterator

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

  8. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  9. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. (转)基于即时通信和LBS技术的位置感知服务(三):搭建Openfire服务器+测试2款IM客户端

    主要包含4个章节: 1. Java 领域的即时通信的解决方案 2. 搭建 Openfire 服务器 3. 使用客户端测试我们搭建的 Openfire 服务器 4. Smack 和 ASmack 一.J ...

  2. DJANGO结合jQuery cxSelect 作二级菜单过滤

    EN,到这个阶段,基本功能算是完成了. 使用了jQuery cxSelect这个插件. http://code.ciaoca.com/jquery/cxselect/ 相关代码如下: html: &l ...

  3. Android的Handler几种常见的传值方式

    public class handlerThread2 extends Activity { @Override protected void onCreate(Bundle savedInstanc ...

  4. GCC警告选项例解

    程序员是追求完美的一族,即使是一般的程序员大多也都不想看到自己的程序中有甚至那么一点点的瑕疵.遇到任意一条编译器警告都坚决不放过.有人会说:我们可以使用比编译器更加严格的静态代码检查工具,如splin ...

  5. Android开发之动画(转)

    activity跳转的过渡效果,很漂亮,很全 注意,切换方法overridePendingTransition只能在startActivity和finish方法之后调用. 第一个参数为第一个Activ ...

  6. Android开发UI之textview实现高亮显示并点击跳转

    textview实现高亮显示,带下划线,带背景,主要是通过SpannableString类实现. 具体实现请看代码: TextView showMoreContent=(TextView)findvi ...

  7. 基于邻接矩阵的广度优先搜索遍历(BFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<stdio.h> #incl ...

  8. SCADA软件整体架构

    SCADA软件整体框架如下所示: 1.免费版本可以支持的IO容量为2048点,无运行时间限制. 2.免费版本仅支持本地Runtime运行,CLServer服务器只能运行24小时. 3.免费版本支持的驱 ...

  9. POI 操作(新接口)

    POI 生成XLS实例 转载至:http://www.4ucode.com/Study/Topic/697242 ackage test; import java.io.FileOutputStrea ...

  10. Using SharePoint 2010 dialogs

    转:http://www.techbubbles.com/sharepoint/using-sharepoint-2010-dialogs/ SharePoint 2010 dialogs are J ...