首页 > 其他 > 详细

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

时间:2019-10-20 11:09:01      阅读:96      评论:0      收藏:0      [点我收藏+]
 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\n",deep(root));
53     }
54     return 0;
55 }

 

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

原文:https://www.cnblogs.com/Angfe/p/11706892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!