# Definition for a  binary tree node
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class BSTIterator(object):
def __init__(self, root):
"""
:type root: TreeNode
"""
self.stack = []
self.pushLeft(root) def hasNext(self):
"""
:rtype: bool
"""
return self.stack def next(self):
"""
:rtype: int
"""
top=self.stack.pop()
self.pushLeft(top.right)
return top.val
def pushLeft(self,node):
while node:
self.stack.append(node)
node=node.left # Your BSTIterator will be called like this:
# i, v = BSTIterator(root), []
# while i.hasNext(): v.append(i.next())

leetcode Binary Search Tree Iterator python的更多相关文章

  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] Binary Search Tree Iterator 深度搜索

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

  6. 【leetcode】Binary Search Tree Iterator

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

  7. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

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

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

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

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. Spark里面的任务调度:离SparkContext开始

    SparkContext这是发达国家Spark入学申请,它负责的相互作用和整个集群,它涉及到创建RDD.accumulators and broadcast variables.理解力Spark架构, ...

  2. 【最大点独立集】【poj1419】【Graph Coloring】

    题意: 最多能选取多少点,没有边相连. 解法: 取反图,求最大团 代码: #include<cstdio> #include<cstring> #include<iost ...

  3. UIScrollView基本使用

    - (void)viewDidLoad { [super viewDidLoad]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMa ...

  4. python中调用C++写的动态库

    一.环境:Windows XP + Python3.2 1. dll对应的源文件(m.cpp): #include <stdio.h> extern "C" { _de ...

  5. Facebook发布C++ HTTP框架Proxygen

    Facebook 宣布发布C++ HTTP 框架 Proxygen,其中包括了一个 HTTP server.Proxygen 是 oxygen 的谐音,支持 SPDY/3 和 SPDY/3.1,未来还 ...

  6. Thinkphp3.2使用scws中文分词 提取关键词

    SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统).1.下载scws官方提供的类(这里使用的是pscws第四版的)http://www ...

  7. mysql性能优化学习笔记(5)数据库结构优化

    一.选择合适的数据类型    1.使用可存下数据的最小的数据类型    2.使用简单地数据类型,Int<varchar    3.尽可能使用not null定义字段    4.尽量少用text, ...

  8. pycharm Run/Debug Configrations

    操作系统是win10,今天维护scrapy爬虫的时候发现pycharm调试配置失效了,导致花了好大力气去搜索配置的路径.在这儿记录下来方便以后查看. Script:C:\Python27\Lib\si ...

  9. meteor学习

    meteor学习 描述:是一套完整的用于开发现代化跨平台实时应用的整体解决方案 不是IDE(集成开发环境) 不是API接口 不是前端框架 不是后端框架 包含 命令行工具 meteor command ...

  10. 《零基础学习Python》01

    前言 Python是一种跨平台的语言 安装Python(Mac OS X) 1.在www.python.org/downloads 中下载Python,然后像安装其他软件一样进行安装. 2.打开: 找 ...