Binary Search Tree Iterator(tree inorder)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Callingnext()
will return the next smallest number in the BST.
Note:next()
andhasNext()
should run in average O(1) time and uses O(h) memory, wherehis the height of the tree.
分析
inorder
iterative: hasnext里是while的条件 next里是遍历的过程。要拆开。想象不断调用hasnext 就是不断while
recursive的话就是直接Inorder做完存在List里。然后控制List的index
follow up: post order,一样遍历,就是捕捉stack pop的瞬间
Last updated