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. 安卓 DevOps:从一次推送命令到生产

    DevOps 是一种广为人知的活动,其主要目的是使软件交付自动化.的确,DevOps 的目标是持续测试.代码质量.功能开发和更轻松地进行维护更新.因此,DevOps 的终极目标之一是让开发者可以执行快 ...

  2. 深入浅出 Java 8 Lambda 表达式

    摘要:此篇文章主要介绍 Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等.本文系 OneAPM 工程师编译整理. Java 是一流的面向对象语言,除了部分简 ...

  3. android应用崩溃的调试方法(c++ lib so文件库崩溃)

    android调试工具addr2line使用: 1.将ndk中的arm-linux-androideabi-addr2line可执行文件的路径加入配置文件~/.bashrc中,例如: export P ...

  4. Servlet课程0426(九)Servlet服务器端创建Cookie和客户端读取Cookie

    服务器端创建Cookie: Win7默认Cookie位置 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies Cookie ...

  5. (转)Android系统自带样式(@android:style/)

    在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不 ...

  6. HDU1171——Big Event in HDU(母函数)

    Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...

  7. Android开发之MediaPlayer和SurfaceView组成视频播放器

    SurfaceView 使用双缓冲技术 是个重量级的组件 只要不可见,就不会创建,可见时,才会创建 只要不可见,就会销毁 SurfaceView一旦不可见,就会被销毁,一旦可见,就会被创建,销毁时停止 ...

  8. 8款JS框架比较

    Dojo     Dojo 是目前最为强大的JS框架,它在自己的 Wiki 上给自己下了一个定义,Dojo 是一个用 JavaScript 编写的开源的DHTML工具箱.Dojo 很想做一个“大一统” ...

  9. centos-nginx添加模块(无需重新编译)

    $ wget http://nginx.org/download/nginx-1.3.2.tar.gz $ .tar.gz #查看ngixn版本极其编译参数 $ /usr/local/nginx/sb ...

  10. 新功能:Azure Traffic Manager 嵌套配置文件

    Jonathan Tuliani  Azure 网络 - DNS 和 Traffic Manager 项目经理 我们很高兴地宣布,Azure Traffic Manager 支持 Traffic Ma ...