Created
November 23, 2011 08:59
-
-
Save jkff/1388235 to your computer and use it in GitHub Desktop.
Binary tree postorder -> structure
This file contains 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
#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