码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Binary Search Tree Iterator python

时间:2015-12-06 22:45:41      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

# 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

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5024566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!