Skip to content

Instantly share code, notes, and snippets.

@jitsceait
Created May 4, 2014 05:32
Show Gist options
  • Select an option

  • Save jitsceait/e38808f1d28adc059e57 to your computer and use it in GitHub Desktop.

Select an option

Save jitsceait/e38808f1d28adc059e57 to your computer and use it in GitHub Desktop.
This program prints tree in zig zag fashion
void zigzag(Node * root){
stack s1, s2;
s1.top = -1;
s2.top = -1;
if(root == NULL ) return;
push(&s1, root);
while(!is_empty(s1) || !is_empty(s2)){
printf("\n");
while(!is_empty(s1)){
Node * temp = pop(&s1);
printf("%d " , temp->value);
if(temp->right)
push(&s2, temp->right);
if(temp->left)
push(&s2, temp->left);
}
printf("\n");
while(!is_empty(s2)){
Node * temp = pop(&s2);
printf("%d " , temp->value);
if(temp->left)
push(&s1, temp->left);
if(temp->right)
push(&s1, temp->right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment