kevi793
BAN USERAlgorithm :-
1.)Start from the root node of the tree.
2.)If the node has left child , store the node in a stack ,mark its right as unvisited in another stack and move to its left . Continue this step until you can find a left child.
3.)Now mark the current node's right child as visited and move to the right child. If the right child is empty , print the content of the stack , else go to step 2.
void root2leaf(node* tree){
struct stack{
node* arr[100];
int visited[100];
int tos;
};
stack st;
st.tos = -1;
do{
while(tree!='\0'){
st.arr[++st.tos]=tree;
st.visited[st.tos]=0;
tree=tree->left;
}
tree=st.arr[st.tos];
st.visited[st.tos]=1;
tree=tree->right;
if(tree=='\0'){
for(int i=0;i<=st.tos;i++){
printf("%d ",st.arr[i]->info);
}
printf("\n");
st.tos-=1;
while(st.visited[st.tos]==1){
st.tos-=1;
}
}
}while(st.tos!=-1);
}
if the 1d array is inorder traversal of tree then it will be ok but what if the array given is either preorder or postorder traversal of the tree , how to handle them ???
- kevi793 May 15, 2014