Binary Search Tree Iterator 07/28
Design an iterator over a binary search tree with the following rules:
Elements are visited in ascending order (i.e. an in-order traversal)
next()
andhasNext()
queries run in O(1) time in average.
Have you met this question in a real interview? Yes
Example
For the following binary search tree, in-order traversal by using iterator is [1, 6, 10, 11, 12]
Challenge
Extra memory usage O(h), h is the height of the tree.
Super Star: Extra memory usage O(1)空间和时间复杂度 不满足条件
Morris Traversal 非递归,不用stack,O(1 )空间复杂度
Last updated