[LeetCode] 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
题目的意思是给定一个bst,将全部数值 升序排列,next 从左到右输出一个值,havenext 输出时候还有未输出的值,需要做的是在class 内部维护这样的输出,同时满足一定的条件。
#include <iostream>
#include <stack>
using namespace std; /**
* Definition for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class BSTIterator {
public:
stack<TreeNode *> stk;
BSTIterator(TreeNode *root) {
TreeNode * node = root;
while(node!=NULL){
stk.push(node);
node = node->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return stk.size()!=;
} /** @return the next smallest number */
int next() {
int retNum = (stk.top())->val;
TreeNode* node = (stk.top())->right;
stk.pop();
while(node!=NULL){
stk.push(node);
node = node->left;
}
return retNum;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/ int main()
{
return ;
}
[LeetCode] 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] 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
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- LeetCode——Binary Search Tree Iterator
Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- leetcode Binary Search Tree Iterator python
# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【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 (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- python基础 字典排序
stus = [ {"name":"zhang","age":18}, {"name":"lisi" ...
- python字典形list 去重复
data_list = [{"}] run_function = lambda x, y: x if y in x else x + [y] return reduce(run_functi ...
- 使用vscode开发vue cli 3项目,配置eslint以及prettier
初始化项目时选择eslint-config-standard作为代码检测规范,vscode安装ESLint和Prettier - Code formatter两个插件,并进行如下配置 { " ...
- 【CodeBase】通过层级键在多维数组中获取目标值
通过层级键在多维数组中获取目标值 /* *Author : @YunGaZeon *Date : 2017.08.09 *param data : Data Array *param keys : K ...
- windows环境下安装npm、cnpm、bower
什么是npm.cnpm.bower? 简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的.那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用 ...
- JavaScript事件对象与事件的委托
事件对象 包含事件相关的信息,如鼠标.时间.触发的DOM对象等 js默认将事件对象封装好,并自动的以参数的形式,传递给事件处理函数的第1个参数,如下: document.getElementsByTa ...
- 笔记1 python入门学习笔记
目录 官方手册 菜鸟站手册地址: python的运行方法 注释 小技巧: input()接收用户输入的内容(默认为字符串) print() 运算符 is 是判断两个标识符是不是引用自一个对象 all和 ...
- 使用Visual Studio建立报表--C#
原文:使用Visual Studio建立报表--C# 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_23893313/article/deta ...
- install golang plugin in webstrom
https://github.com/go-lang-plugin-org/go-lang-idea-plugin/wiki/Documentation
- HTML练习题
1.查询一下对div和span标签的理解 div标签:是用来为HTML文档内大块的内容提供结构和背景的元素.DIV的起始标签和结束标签之间的所有内容都是用来构成这个块的,中文我们把它称作“层”. sp ...