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 ...
随机推荐
- web后台知识点整理
五.JEE 适用于创建server端的大型的软件服务系统 1. JEE : JAVA PLATFORM ENTERPRISE DEDITON 2.是一个规范集.技术集.框架集(API集) 一种技 ...
- 2014.04.16,读书,读书笔记-《Matlab R2014a完全自学一本通》-第17章 图形用户界面
界面对象分三类: 用户控件对象(uicontrol) 下拉式菜单对象(uimenu) 内容式菜单对象(uicontextmenu) 创建用户界面: 1.命令行方式 采用uicontrol来创建控件对象 ...
- awesome-free-software
Free software is distributed under terms that allow users to run the program for any purpose, study ...
- 51nod-1462: 树据结构
[传送门:51nod-1462] 简要题意: 给出一棵n个点的树,每个点有两个权值v,t 有Q个操作,有两种操作: 1.将x到根上的路径上的点的v值都加上d 2.将x到根上的路径上的点的t值都加上每个 ...
- poj--3159--Candies(简单差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 26888 Accepted: 7398 Descrip ...
- [AHOI 2009] 同类分布
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1799 [算法] 数位DP [代码] #include<bits/stdc++. ...
- 10. Regular Expression Matching[H]正则表达式匹配
题目 Given an input string(s) and a pattern(p), implement regular expression matching with support for ...
- php.ini控制文件上传大小配置项
; Whether to allow HTTP file uploads.file_uploads = On ; Temporary directory for HTTP uploaded files ...
- HD-ACM算法专攻系列(14)——find your present (2)
问题描述: 源码: #include"iostream" #include"algorithm" using namespace std; bool cmp(i ...
- C#——单元测试
测试搭建请看:http://www.cnblogs.com/Look_Sun/p/4514732.html 右键不出现Generate Unit Test选项请参考:http://www.jb51.n ...