Last active
August 29, 2015 14:00
-
-
Save jitsceait/997fa7e750b4a67bdcff 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 printTreeLevel_iter1(Node *root){ | |
| int h = height(root); | |
| int i; | |
| int ltr = 1; | |
| for(i =1; i<=h; i++){ | |
| printf("\n Level %d :", i); | |
| /* initially passing it to left to right */ | |
| zigzagRec(root, i, ltr); | |
| /* For next iteration (level), it will be reversed */ | |
| ltr = !ltr; | |
| printf("\n"); | |
| } | |
| } | |
| void zigzagRec(Node * node, int desired, int ltr){ | |
| if(node == NULL) return; | |
| if (desired ==1) | |
| printf("%d ", node->value); | |
| /* Based on the flag call the recursive function accordingly */ | |
| if(ltr){ | |
| zigzagRec(node->left, desired -1, ltr); | |
| zigzagRec(node->right, desired -1, ltr); | |
| } | |
| else{ | |
| zigzagRec(node->right, desired -1, ltr); | |
| zigzagRec(node->left, desired -1, ltr); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment