标签:
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.
问题描述:BST的Iterator,next()从小到大依次给出,next(),hasNext() O(1)时间复杂度 ,O(h)内存高度;
1:初始化时一次性中序遍历到arraylist中(体现不出O(h)要求):
2:初始化把树的最左排的节点依次压入stack中,hasNext() stack.isEmpty(),当调用next()时,在把目前节点的右子树的左排节点依次压入
标签:
原文地址:http://www.cnblogs.com/onlysun/p/4419388.html