题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667
5-5 Tree Traversals Again (25分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer NN (le 30≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to NN). Then 2N2Nlines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
这道题是根据输入算出前序遍历和中序遍历,不过没有直接建树去做后序,而是参照前两者遍历的序列,递归分解算了后续出来
/* 评测结果 时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户 2017-07-08 16:00 答案正确 25 5-5 gcc 14 1 测试点结果 测试点 结果 得分/满分 用时(ms) 内存(MB) 测试点1 答案正确 12/12 14 1 测试点2 答案正确 4/4 2 1 测试点3 答案正确 4/4 2 1 测试点4 答案正确 1/1 13 1 测试点5 答案正确 4/4 2 1 */ #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXLEN 50 struct stack{ int data[MAXLEN]; int top; }; typedef struct stack *ptrStack; int printcount=0; int length; ptrStack CreateStack() { ptrStack temp; temp=(ptrStack)malloc(sizeof(struct stack)); temp->top=0; return temp; } void Push(ptrStack s,int item) { s->data[++(s->top)]=item; } int Pop(ptrStack s) { return s->data[(s->top)--]; } void DestroyStack(ptrStack s) { free(s); } void Process(int preOrder[],int inOrder[],int preStart,int preEnd,int inStart,int inEnd) { int i,root,mid,leftsize,rightsize; int nextLeftpreStart,nextLeftpreEnd,nextLeftinStart,nextLeftinEnd; int nextRightpreStart,nextRightpreEnd,nextRightinStart,nextRightinEnd; if(preStart==preEnd){ printf("%d",preOrder[preStart]); printcount++; if (printcount!=length) putchar(' '); return; } root=preOrder[preStart]; for(i=inStart;i<=inEnd;i++) if(inOrder[i]==root) break; leftsize=i-inStart; rightsize=inEnd-i; nextLeftpreStart=preStart+1; nextLeftpreEnd=preStart+leftsize; nextRightpreStart=nextLeftpreEnd+1; nextRightpreEnd=nextLeftpreEnd+rightsize; nextLeftinStart=i-leftsize; nextLeftinEnd=i-1; nextRightinStart=i+1; nextRightinEnd=i+rightsize; if(i!=inStart){ Process(preOrder,inOrder,nextLeftpreStart,nextLeftpreEnd,nextLeftinStart,nextLeftinEnd); } if(i!=inEnd){ Process(preOrder,inOrder,nextRightpreStart,nextRightpreEnd,nextRightinStart,nextRightinEnd); } printf("%d",root); printcount++; if (printcount!=length) putchar(' '); return; } void Input(int pre[],int in[]) { int i,n,num,idxforpush=0,idxforpop=0; char* s; char temp[50]; ptrStack workstack=CreateStack(); scanf("%d",&n); length=n; n=n*2; for(i=0;i<n;i++) { scanf("%s",temp); s=strchr(temp,'h'); if(s!=NULL){ scanf("%d",&num); pre[idxforpush]=num; Push(workstack,num); // printf("----pre----%d ",pre[idxforpush]);//for test idxforpush++; } else{ in[idxforpop]=Pop(workstack); // printf("++++ in++++%d ",in[idxforpop]);//for test idxforpop++; } } DestroyStack(workstack); } int main() { int i; int in[MAXLEN]; int pre[MAXLEN]; Input(pre,in); // for(i=0;i<length;i++) printf("idx %d in %d pre %d ",i,in[i],pre[i]);//for test; Process(pre,in,0,length-1,0,length-1); }