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: Stack
用一个stack先预存排序了的treenode, 每次要调用next()和hasNext()的时候,只需要对stack操作即可。
C++
Java
解法2: Stack, 非递归的in order traversal
实际上不需要预先存储排了序的treenode,而是在寻找最小的值的时候把经过的node用stack存起来。
这里实际上考的是iterative的in order traversal
C++