[Leetcode] Binary search tree --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.

1)Solution:

Getting the next iterator is similarly to call  the generator in the python. for the next smallest number, it‘s like the inorder tree traversal printed out (sorted order). we use stack the store the node visited while using iterators

so the next could pop the node in order from stack.

 1     def __init__(self, root):
 2         """
 3         :type root: TreeNode
 4         """
 5         self.stk = []
 6         self.getAllLeftNode(root)
 7
 8     def hasNext(self):
 9         """
10         :rtype: bool
11         """
12
13         return (len(self.stk) != 0)
14
15     def next(self):
16         """
17         :rtype: int
18         """
19         #pop from stack
20         node = self.stk.pop()
21         self.getAllLeftNode(node.right)
22         return node.val
23
24     def getAllLeftNode(self, node):
25         ‘‘‘
26         get the most left nodes and put in the stack
27         ‘‘‘
28         while (node is not None):
29             self.stk.append(node)
30             node = node.left

原文地址:https://www.cnblogs.com/anxin6699/p/8242225.html

时间: 2024-10-08 18:43:30

[Leetcode] Binary search tree --Binary Search Tree Iterator的相关文章

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

leetcode第一刷_Recover Binary Search Tree

这是一道好题,思路虽然有,但是提交之后总是有数据过不了,又按照数据改改改,最后代码都没法看了.收到的教训是如果必须为自己的代码加上很多很多特殊的限定,来过一些特殊的数据的话,说明代码本身有很大的漏洞. 这道题,我想到了要用两个指针保存乱序的节点,甚至想到了用一个pre指针来保存前面一个节点,但是问题出在哪里呢?我觉得应该是自己对树的遍历理解的不够深刻.既然知道了二叉搜索树一定是用中序遍历的,那么程序的框架应该马上写的出来,先左子树,再根,再右子树,那你说什么时候更新pre指针呢,当然是访问根节点

[Leetcode] Binary search -- 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

leetcode || 98、Validate Binary Search Tree

problem: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

leetcode || 99、Recover Binary Search Tree

problem: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#

leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal

problem: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:利用二叉树的前序遍历序列和中序遍历序列 构造二叉树,很经典的一道题 thinking: (1)前序遍历的顺序是:父节点-

[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search 简单的深度搜索 #include <iostream> using namespace std; /** *

leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal

problem: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:给定二叉树的中序遍历序列和后续遍历序列,构建这棵二叉树,也很经典 thinking: (1)这种类型的题,要举个例子找

leetcode——Maximum Depth of Binary Tree (递归,)

Maximum Depth of Binary Tree Total Accepted: 59837 Total Submissions: 132940My Submissions Question Solution Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the f

leetcode || 110、Balanced Binary Tree

problem: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Hide Tags Tree Depth-first