首页 技术 正文
技术 2022年11月14日
0 收藏 436 点赞 5,115 浏览 1335 个字
 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef char ElemType; typedef struct BinTree {     ElemType data;     struct BinTree *left;     struct BinTree *right; }BinTree; bool TreeCmp(BinTree * a, BinTree * b); //判断a树与b树是否同构 BinTree * BinTree_new(int n); int main() {     int n;     scanf("%d", &n);     BinTree * a = BinTree_new(n);     scanf("%d", &n);     BinTree * b = BinTree_new(n);     bool flag = TreeCmp(a, b);     if (flag)         printf("Yes");     else         printf("No");     ; } BinTree * BinTree_new(int n) {     )     {         return  NULL;     }     else     {         int i, li, ri;         ElemType c, lc, rc;         BinTree * T = (BinTree *)malloc(n * sizeof(BinTree));         bool *head = (bool *)malloc(n * sizeof(bool));         memset(head, true ,sizeof(head));         ; i < n ; ++i )         {             scanf(" %c %c %c", &c, &lc, &rc);             li = lc-';             ri = rc-';             T[i].data = c;             if (lc != '-')             {                 T[i].left = &T[li];                 head[li] = false;             }             else                 T[i].left = NULL;             if (rc != '-')             {                 T[i].right = &T[ri];                 head[ri] = false;             }             else                 T[i].right = NULL;         }         //寻找树的树根(树根没有其它的结点指向它)         ; i < n ; ++i )             if (head[i] == true)                 break;         free(head);         return &T[i];     } } bool TreeCmp(BinTree * a, BinTree * b) {     if (a == NULL && b == NULL)//两棵树都为空         return true;     if (a == NULL || b == NULL)//有一棵树为空         return false;     if (a->data != b->data)//树结点的值不相等         return false;     if (a->left == NULL && b->left == NULL)//两棵树的左子树都为空         return TreeCmp(a->right, b->right);//就比较右子树     //两棵树的左子树都不为空,且两个值相等就比较两棵树的子树     if (a->left != NULL && b->left != NULL &&(a->left->data == b->left->data))         return (TreeCmp(a->left,b->left)&&TreeCmp(a->right,b->right));     //交换后再比较     else         return (TreeCmp(a->left,b->right)&&TreeCmp(a->right,b->left)); }
上一篇: table表格的属性
下一篇: Jump的计划
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300