二叉树由先序遍历和中序遍历输出后序遍历

虽然实质和LRJ那个差不多,但感觉这样写符合自己的思维。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void print(char *s1, char *s2, int n)//这是主要函数,s1指向先序遍历,s2则中序,n为长度
 5 {
 6    char *st = strchr(s2,s1[0]);
 7    if(n==0)
 8      return;
 9    print(s1+1,s2,st-s2);
10    print(s1+1+(st-s2),st+1,n-1-(st-s2));
11    printf("%c",s1[0]);
12 }
13 
14 int main()
15 {
16    char s1[]="DBACEGF";//先序遍历
17    char s2[]="ABCDEFG";//中序遍历
18    print(s1,s2,7);
19    printf("\n");
20    return 0;
21 }

另外要能够举一反三,自己练完由中、后写出前序来。。

顺便考虑着UVA TREE。

posted @ 2013-02-20 21:50  闭关修炼的小孩纸  阅读(185)  评论(0编辑  收藏  举报