二叉树非递归 后序遍历

 1#include<iostream>
 2#include<stack>
 3using namespace std;
 4
 5struct BinTreeNode{
 6    int data;
 7    BinTreeNode *left;
 8    BinTreeNode *right;
 9}
;
10enum tagtype{L,R};
11struct StackElem{
12    BinTreeNode *ptr;
13    tagtype tag;
14}
;
15
16void PostOrder(BinTreeNode *root){
17    stack<StackElem> s;
18    StackElem x;
19    BinTreeNode *tmp = root;
20    while(tmp!=NULL || !s.empty()){
21        while(tmp != NULL){
22            x.ptr = tmp;
23            x.tag = L;
24            s.push(x);
25            tmp = tmp->next;
26        }

27        while(!s.empty() && s.top().tag == R){
28            cout<<s.top().ptr->data<<' ';
29            s.pop();
30        }

31        if(!s.empty()){
32            s.top().tag = R;
33            tmp = s.top().ptr->right;
34        }

35    }

36}

posted on 2011-08-18 15:32 Hsssssss 阅读(130) 评论(0)  编辑 收藏 引用 所属分类: C++代码


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论