二叉树非递归 后序遍历

 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 阅读(157) 评论(0)  编辑 收藏 引用 所属分类: C++代码

<2025年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论