Created
March 11, 2014 16:58
-
-
Save qiuwei/9490101 to your computer and use it in GitHub Desktop.
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
| // construct the post transverse based on the preorder and inorder transverse | |
| // lrj whitebook p106 | |
| #include <iostream> | |
| #include <cstdio> | |
| #include <cstring> | |
| #include <cassert> | |
| using namespace std; | |
| const int MAXN = 100; | |
| void postorder(char* pre, char* pre_end, char *in, char *in_end){ | |
| if(pre > pre_end){ | |
| return; | |
| } | |
| if(pre == pre_end){ | |
| cout << *pre << " "; | |
| }else{ | |
| char * index = strchr(in, *pre); | |
| postorder(pre+1, pre+(index-in), in, index-1); | |
| postorder(pre+(index-in)+1, pre_end, index+1, in_end); | |
| cout << *index << " "; | |
| } | |
| } | |
| int main(){ | |
| char prebuf[MAXN]; | |
| char inbuf[MAXN]; | |
| int len = 0; | |
| while(scanf("%s%s", prebuf, inbuf) != -1){ | |
| //printf("%s %s\n", prebuf, inbuf); | |
| len = strlen(prebuf); | |
| postorder(prebuf, prebuf+len-1, inbuf, inbuf+len-1); | |
| cout << endl; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment