3·编写后序遍历二叉树的非递归算法。
// 非递归后序遍历
void postorderTravel(Tree T){
if(T==NULL){
return;
}
stack< pair<Node *, bool> > S;
S.push(make_pair(T, false));
bool isPassed;
while(!S.empty()){
pair<Node *, bool> p = S.top();
S.pop();
isPassed = p.second;
if(isPassed){
visit(p.first);
}else{
S.push(make_pair(p.first, true));
if((p.first)->right){
S.push(make_pair((p.first)->right, false));
}
if((p.first)->left){
S.push(make_pair((p.first)->left, false));
}
}
}
}