Iterative post order

on Saturday, December 5, 2009


Write a code which will traverse the tree in post-order fashion, iteratively.













SOLUTION

void traverse( tree *root){
Stack st;
while ( !st.empty() && root != NULL){
while( root){
st.push(root);
root = root->left;
}
if ( st.top() ){
st.push(NULL);
root=root->right;
}
else{
st.pop();
print st.top()->data;
st.pop();
root = NULL;
}
}
}

0 comments:

Post a Comment