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.

Hide Tags

Tree Stack

 

  题目的意思是给定一个bst,将全部数值 升序排列,next 从左到右输出一个值,havenext 输出时候还有未输出的值,需要做的是在class 内部维护这样的输出,同时满足一定的条件。
  方法是通过stack 来维护。
 
#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 深度搜索的更多相关文章

  1. LeetCode: Binary Search Tree Iterator 解题报告

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

  2. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

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

  3. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  4. LeetCode——Binary Search Tree Iterator

    Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...

  5. [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 ...

  6. leetcode Binary Search Tree Iterator python

    # Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...

  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 (2 solutions)

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

  9. leetcode-173:Binary Search Tree Iterator(Java)

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

随机推荐

  1. 小象学院Python数据分析第二期【升级版】

    点击了解更多Python课程>>> 小象学院Python数据分析第二期[升级版] 主讲老师: 梁斌 资深算法工程师 查尔斯特大学(Charles Sturt University)计 ...

  2. python笔记-dict字典的方法2

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' 概述: 使用键值(key-value)存储,具有极快的查找速度 注意:字典是无序的 key的特性: 1. ...

  3. Ansible学习 Inventory文件

    Ansible可同时操作属于一个组的多台主机,组与主机之间关系配置在inventory文件中,inventory默认的配置文件是/etc/ansible/hosts 1.在/etc/ansible/h ...

  4. day 71 Django基础六之ORM中的锁和事务

    Django基础六之ORM中的锁和事务   本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update(no ...

  5. GoF23种设计模式之创建型模式之抽象工厂模式

    一.概述 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 二.适用性 1.一个系统要独立于它的产品的创建.组合和表示的时候. 2.一个系统要由多个产品系列中的一个来配置的时候. ...

  6. JAVA运行环境配置

    win10下,右击我的电脑-->高级系统设置-->高级-->环境变量-->系统变量 1新建 变量名   JAVA_HOME 变量值   C:\Program Files\Jav ...

  7. 8、python中的集合

    集合是python中无序.可变的数据结构.集合与字典类似,集合中的元素必须是可哈希的(等同于字典中的键),也就是说集合中的元素是唯一.不可变的数据类型.这里前面说集合可变,后面又说集合中的元素不可变是 ...

  8. HDU 4919 Exclusive or 数学

    题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...

  9. Python-S9-Day87——admin的使用

    01 admin的使用1 02 admin的使用2 03 admin的使用3 04 url方法的使用 05 单例模式 06 admin源码之注册功能 07 admin源码之url设计 08 admin ...

  10. Python人工智能-基于百度AI接口

    参考百度AI官网:http://ai.baidu.com/ 准备工作: 支持Python版本:2.7.+ ,3.+ 安装使用Python SDK有如下方式 >如果已经安装了pip,执行 pip ...