2804=数据结构实验之二叉树八:(中序后序)求二叉树的深度

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 struct node
 5 {
 6     char a;
 7     struct node*left,*right;
 8 };
 9 int max(int a,int b)
10 {
11     if(a>b)return a;
12     else return b;
13 }
14 struct node *creat(int n,char s1[],char s2[] )
15 {
16     if(n==0)return NULL;
17     struct node *root;
18     root=(struct node*)malloc(sizeof(struct node));
19     int i;
20     for(i=0;i<n;i++)
21     {
22         if(s1[i]==s2[n-1])break;
23     }
24     root->left = creat(i,s1,s2);
25     root->right = creat(n-i-1,s1+i+1,s2+i);
26     return root;
27 };
28 int deep(struct node*root)
29 {
30     int h;
31     int d1,d2;
32     if(root)
33     {
34         d1=deep(root->left);
35         d2=deep(root->right);
36         h=max(d1+1,d2+1);
37     }
38     return h;
39 }
40 int main()
41 {
42     struct node*root;
43     char s2[1000],s1[1000];
44     int n,len;
45     scanf("%d",&n);
46     while(n--)
47     {
48         scanf("%s%s",s1,s2);
49         len=strlen(s1);
50         root=(struct node*)malloc(sizeof(struct node));
51         root=creat(len,s1,s2);
52         printf("%d
",deep(root));
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/Angfe/p/11706892.html