码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Search Tree Iterator

时间:2015-04-12 14:38:25      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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()时,在把目前节点的右子树的左排节点依次压入

技术分享

 

Binary Search Tree Iterator

标签:

原文地址:http://www.cnblogs.com/onlysun/p/4419388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!