-
Notifications
You must be signed in to change notification settings - Fork 0
/
InPre2Post.cpp
executable file
·34 lines (33 loc) · 996 Bytes
/
InPre2Post.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cstring>
void InPre2Post(const char * pInOrder,const char * pPreOrder,int nLength,char * pPostOrder,int &nIndex) {
if(nLength <= 0)
return;
else if(nLength == 1) {
pPostOrder[nIndex] = *pPreOrder;
nIndex++;
return;
}
char root = *pPreOrder;
int nRoot = 0;
for(;nRoot < nLength;nRoot++) {
if(root == pInOrder[nRoot])
break;
}
InPre2Post(pInOrder,pPreOrder + 1,nRoot,pPostOrder,nIndex);
InPre2Post(pInOrder + nRoot + 1,pPreOrder + nRoot + 1,nLength - nRoot - 1,pPostOrder,nIndex);
pPostOrder[nIndex++] = root;
}
int main() {
using namespace std;
char pPreOrder[] = "GDAFEMHZ";
char pInOrder[] = "ADEFGHMZ";
int size = strlen(pPreOrder);
char * pPostOrder = new char[size + 1];
int nIndex = 0;
InPre2Post(pInOrder,pPreOrder,size,pPostOrder,nIndex);
pPostOrder[size] = '\0';
cout<<pPostOrder<<endl;
delete [] pPostOrder;
return 0;
}