Created
May 4, 2014 05:32
-
-
Save jitsceait/e38808f1d28adc059e57 to your computer and use it in GitHub Desktop.
This program prints tree in zig zag fashion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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