void BT_InOrderNoRec(pTreeT root){ stack<treeT *> s; while ((NULL != root) || !s.empty()) { if (NULL != root) { s.push(root); root = root->left; } else { root = s.top(); visit(root); s.pop(); root = root->right; } }}自己实现代码(比较粗糙):
顺序访问每个节点,然后将右节点插入栈中。然后将当前节点变换为左节点。知道当前节点为空,才会作出栈操作。伪代码如下: void BT_PreOrderNoRec(pTreeT root){ stack<treeT *> s; while ((NULL != root) || !s.empty()) { if (NULL != root) { visit(root); s.push(root); root = root->left; } else { root = s.top(); s.pop(); root = root->right; } }}自己实现的代码:
posted on 2011-04-10 10:42 kahn 阅读(303) 评论(0) 编辑 收藏 引用 所属分类: 算法相关
Powered by: C++博客 Copyright © kahn