zoukankan      html  css  js  c++  java
  • 173. 二叉搜索树迭代器

    实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。

    调用 next() 将返回二叉搜索树中的下一个最小的数。

    示例:

    BSTIterator iterator = new BSTIterator(root);
    iterator.next();    // 返回 3
    iterator.next();    // 返回 7
    iterator.hasNext(); // 返回 true
    iterator.next();    // 返回 9
    iterator.hasNext(); // 返回 true
    iterator.next();    // 返回 15
    iterator.hasNext(); // 返回 true
    iterator.next();    // 返回 20
    iterator.hasNext(); // 返回 false

    提示:

    • next() 和 hasNext() 操作的时间复杂度是 O(1),并使用 O(h) 内存,其中 是树的高度。
    • 你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 中至少存在一个下一个最小的数。
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class BSTIterator:
        def preorderTraversal(self, root):
            if not root:return []
            stack, res = [root], []
            while stack:
                root = stack.pop()
                if root:
                    res.append(root.val)
                    if root.right:
                        stack.append(root.right)
                    if root.left:
                        stack.append(root.left)
            return res
    
        def __init__(self, root: TreeNode):
            self.nodes=sorted(self.preorderTraversal(root))
    
        def next(self) -> int:
            """
            @return the next smallest number
            """
            if self.hasNext():
                return self.nodes.pop(0)
            else:
                return None
    
        def hasNext(self) -> bool:
            """
            @return whether we have a next smallest number
            """
            return len(self.nodes)>0
    
    
    
    # Your BSTIterator object will be instantiated and called as such:
    # obj = BSTIterator(root)
    # param_1 = obj.next()
    # param_2 = obj.hasNext()
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    class BSTIterator:
    
        def __init__(self, root: TreeNode):
            self.stack = []
            while root:
                self.stack.append(root)
                root = root.left
    
        def next(self) -> int:
            """
            @return the next smallest number
            """
            node = self.stack.pop()
            r = node.right
            while r:
                self.stack.append(r)
                r = r.left
            return node.val
    
        def hasNext(self) -> bool:
            """
            @return whether we have a next smallest number
            """
            return len(self.stack) > 0
    
    
    # Your BSTIterator object will be instantiated and called as such:
    # obj = BSTIterator(root)
    # param_1 = obj.next()
    # param_2 = obj.hasNext()
  • 相关阅读:
    C#引用类型详细剖析(转)
    wcf问题集锦
    Emgu CV 初试
    C#语言使用习惯
    多线程和消息机制
    ArrayAdapter
    SimpleAdapter
    删除对话框
    HTML制作个人简历
    冒泡排序
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13936546.html
Copyright © 2011-2022 走看看