leetCode(24):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:
BSTIterator(TreeNode *root) {
i=0;
if(NULL==root)
{
length=0;
}
else
{
stack<TreeNode*> nodes;
TreeNode* cur=root;
while(!nodes.empty() || cur)
{
while(cur)
{
nodes.push(cur);
cur=cur->left;
}
cur=nodes.top();
nodesValue.push_back(cur->val);
nodes.pop();
cur=cur->right;
}
length=nodesValue.size();
}
} /** @return whether we have a next smallest number */
bool hasNext() {
if(i<length)
return true;
return false;
} /** @return the next smallest number */
int next() {
return nodesValue[i++];
}
private:
vector<int> nodesValue;
int i;
int length;
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
leetCode(24):Binary Search Tree Iterator的更多相关文章
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 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 设计迭代器(搜索树)--------- 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 ...
- 【leetcode】Binary Search Tree Iterator(middle)
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 ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【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: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- rabbitMQ学习笔记(二) 简单的发送与接收消息 HelloWorld
首先要下载rabbitmq的javaClient库,然后加入到项目中,下载地址为:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1. ...
- (十一)Unity5新特性----实战2D游戏
孙广东 2015.7.11 在本教程中,将了解到U5新功能.你通过本教程.您将了解下面内容: Changes in Component Access Physics Effectors Adding ...
- 易语言VS杀毒软件:基情复燃,转受为攻!
这两天在编程领域里,又发生了一件令人瞩目的事情:以易语言创始人吴涛的一篇檄文<tid=370327">让我们一起来对360误报说不!>为序幕.再次拉开了易语言跟360杀毒软 ...
- UVA 11609 - Anne's game cayley定理
Lily: “Chantarelle was part of my exotic phase.”Buffy: “It’s nice. It’s a mushroom.”Lily: “It is? Tha ...
- display:block jquery.sort()
对所有的块元素都没有意义,块元素的dispaly属性默认值为block,没必要再显式定义——除非你之前对块元素的display属性重新定义过.===========================多罗 ...
- spring-boot系列:(一)整合dubbo
spring-boot-2整合dubbo 新框架学习,必须上手干.书读百遍,其义自见. 本文主要介绍spring-boot-2整合dubbo,使用xml配置实现一个provider和consumer. ...
- 算法入门经典-第六章 例题6-21 SystemDependencies
题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上.你的任务是模拟安装和卸载软件组件的过程.有以下五种指令,如果指令为“END”则退出程序:若为以下四种指令,则 ...
- composer的一些操作
版本更新 命令行下:composer self-update 设置中国镜像 composer config -g repo.packagist composer https://packagist.p ...
- 指定的WSDL可能与所选的工具包不兼容
使用LoadRunner在webservice协议下,import service的时候,报错: can not fetch WSDL,the specified WSDL datathe speci ...
- Unity 指定区域随机实例化预制体Prefab 代码
using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject prefab; void ...