Skip to content

Instantly share code, notes, and snippets.

@jkff
Created November 23, 2011 08:59
Show Gist options
  • Save jkff/1388235 to your computer and use it in GitHub Desktop.
Save jkff/1388235 to your computer and use it in GitHub Desktop.
Binary tree postorder -> structure
#include <stdio.h>
void printTree(int *idx, int *post, int rootIdx, int n) {
int root = post[rootIdx];
printf("%d", root);
if(root == n-1 || idx[root+1] > rootIdx) return;
printf(" { ");
printTree(idx, post, idx[root+1], n);
printf(" ; ");
printTree(idx, post, rootIdx - 1, n);
printf(" }");
}
int main() {
int post[] = {2,3,1,6,7,5,8,4,0};
const int n = sizeof(post)/sizeof(*post);
int idx[n], i;
for(i = 0; i < n; ++i) idx[post[i]] = i;
printTree(idx, post, n-1, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment