Skip to content

Instantly share code, notes, and snippets.

@jitsceait
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

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

Select an option

Save jitsceait/997fa7e750b4a67bdcff to your computer and use it in GitHub Desktop.
This program prints tree in zig zag fashion
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