1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| void PostOrder(Bitree* root){ if(root == NULL) return; PostOrder(root -> lchild); PostOrder(root -> rchild); cout << root->data <<" "; }
void PostOrder_Nonrecursive(BiTree* root){ stack<BiTree*> s; BiTree *p = root,*r = NULL, temp; while(p || !s.empty()){ if(p){ s.push(p) p=p->lchild; } else{ p=s.top(); if(p->rchild&&p->rchild!=r){ p=p->rchild; s.push(p); p=p->lchild; } else{ p=s.top(); s.pop(); cout<<p->data; r=p; p=NULL; } } } }
void PostOrder_DoubleStack(BiTree* root){ stack<BiTree*> s1,s2; BiTree curr; s1.push(root); while(!s1.empty()){ curr = s1.top(); s1.pop(); s2.push(curr); if(curr->lchild) s1.push(curr->lchild); if(curr->rchild) s1.push(curr->rchild); }
while(!s2.empty()){ cout<<s2.top()->data<<" "; s2.top(); } }
|