# 帮忙解决一道二叉树题目：根据中序和后序序列重建二叉树得到前序序列

1

PS:我自己提交了10遍，都是TLE(超时)。我用递归重建树，再递归进行前序遍历。TLE!我觉得自己写得没错，只是效率太低，所以超时了。然后前序遍历用非递归写，又TLE！我就贴一下自己写的代码，让高手们看看哪里写得不好，如何改进提高效率.

``` 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stack>
4 using namespace std;
5 #define maxn 65537
6 typedef struct node{
7     int data;
8     struct node *lChild, *rChild;
9 }BiTreeNode, *BiTree;
10
11 BiTree createBiTreeInPreOrd(int * in, int from, int to, int * post, int start, int end);//由中序和后序重建二叉树
12 void preOrdTraversalWithoutRecursion(BiTree T); //前序遍历
13 void visit(BiTree T); //访问并输出结点数据
14 int getRootIndex(int * in, int from, int to, int rootValue);//在中序找根结点
15
16 int main()
17 {
18     int inOrder[maxn], postOrder[maxn], len;
19     char c;
20     len = 0;
21     //输入
22     while(scanf("%d",&inOrder[len++])){
23         scanf("%c",&c);
24         if(c == '\n')break;
25     }
26      len = 0;
27     while(scanf("%d",&postOrder[len++])){
28         scanf("%c",&c);
29         if(c == '\n')break;
30     }
31     //建立二叉树
32     BiTree T = createBiTreeInPreOrd(inOrder, 0, len-1, postOrder, 0, len-1);
33     preOrdTraversalWithoutRecursion(T);
34     printf("\n");
35     return 0;
36 }
37
38 BiTree createBiTreeInPreOrd(int * in, int from, int to, int * post, int start, int en){
39         BiTree T = (BiTree)malloc(sizeof(BiTreeNode));
40         int rIndex = getRootIndex(in, from, to, post[en]);
41         T->data = post[en];
42         if(rIndex == from &&rIndex < to){
43             T->lChild = NULL;
44             T->rChild = createBiTreeInPreOrd(in, rIndex+1, to, post, start, en-1);
45         }
46         else if(rIndex == to && rIndex > from){
47             T->rChild = NULL;
48             T->lChild = createBiTreeInPreOrd(in, from, rIndex-1, post, start, en-1);
49         }else if(rIndex == from && rIndex == to){
50                 T->lChild = NULL;
51                 T->rChild = NULL;
52         }else{
53               T->lChild = createBiTreeInPreOrd(in, from, rIndex-1, post, start, rIndex-1);
54               T->rChild = createBiTreeInPreOrd(in, rIndex+1, to, post, rIndex, en-1);
55         }
56         return T;
57 }
58
59 int getRootIndex(int * in, int from, int to, int rootValue){
60         int i;
61         for(i=from; i<=to; i++){
62             if(in[i] == rootValue)
63                 break;
64         }
65         return i;
66 }
67
68 void preOrdTraversalWithoutRecursion(BiTree T){
69     stack<BiTree>s;
70     BiTreeNode * p = T;
71     while(!s.empty())s.pop();
72     while(p != NULL){
73         visit(p);
74         if(p->rChild!=NULL)
75             s.push(p->rChild);
76         if(p->lChild!=NULL)
77             p = p->lChild;
78         else{
79             if(!s.empty()){
80                 p = s.top();
81                 s.pop();
82             }
83             else break;
84         }
85     }
86 }
87
88 void visit(BiTree T){
89     if(T != NULL){
90         printf("%d ", T->data);
91     }
92 }```

9 5 31 67
9 31 67 5

5 9 67 31

PS:我自己输入信息，输出结果没错啊。

supervsky | 初学一级 | 园豆：144

0

0

void preOrdTraversalWithoutRecursion(BiTree T)
{
if (T==NULL)
{
return;
}
printf("%d ", T->data);
preOrdTraversalWithoutRecursion(T->lChild);
preOrdTraversalWithoutRecursion(T->rChild);
}

Wings_翅膀 | 园豆：204 (菜鸟二级) | 2016-01-10 14:55

您需要登录以后才能回答，未注册用户请先注册