首页 > 其他 > 详细

Tree inorder traversal Iterator

时间:2014-11-24 07:34:12      阅读:250      评论:0      收藏:0      [点我收藏+]

TreeNode a;
while (a.hasNext())
   visit(a.next());
}"
问:给TreeNode写Iterator,使得以上代码可以in order traversal

 

class TreeNode
{
TreeNode *root;
public:
    bool hasNext()
    {
        return root!=NULL || !stack.empty();
    }
    TreeNode * next()
    {
        TreeNode * ret = NULL;
        while(hashNext())
        {
            if(root)
            {
                stack.push(root);
                root = root->left;
            }
            else
            {
                root = stack.top();
                stack.pop();
                ret = root;
                root = root->right;
                return ret;
            }
        }
        return NULL;
    }
};

 

Tree inorder traversal Iterator

原文:http://www.cnblogs.com/jobfindingnotes/p/4117873.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!