zoukankan      html  css  js  c++  java
  • 数据结构实验之二叉树一:树的同构 (SDUT 3340)

    题解:把原本结构体的左右子树的类型定义成 int 型,用来存放这个结点的左右子树的编号,分别建造两棵二叉树,按个比较,如果在第二棵树中没有找到,那么就不用在判断了。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    struct node
    {
        char data;
        int l,r;
    };
    struct node t1[20],t2[20];
    void build(struct node *t, int n)
    {
        for(int i = 0; i < n; i ++)
        {
            char s[55];
            scanf("%s", s);
            t[i].data = s[0];
            scanf("%s", s);
            if(s[0] == '-') t[i].l = 100;
            else t[i].l = s[0] - '0';
            scanf("%s", s);
            if(s[0] == '-') t[i].r = 100;
            else t[i].r = s[0] - '0';
        }
    }
    int ok(int i, int j)
    {
        if(t1[t1[i].l].data == t2[t2[j].l].data && t1[t1[i].r].data == t2[t2[j].r].data)
            return 1;
        else if(t1[t1[i].r].data == t2[t2[j].l].data && t1[t1[i].l].data == t2[t2[j].r].data)
            return 1;
        else return 0;
    }
    int main()
    {
        int n,m;
        while(~scanf("%d",&n))
        {
            build(t1,n);
            scanf("%d",&m);
            build(t2,m);
            int f = 0,i,j;
            for(i = 0; i < n; i ++)
            {
                for( j = 0; j < m; j ++)
                {
                    if(t1[i].data == t2[j].data)
                    {
                        if(ok(i,j)==0){
                            f = 1;
                            break;
                        }
                        else break;
                    }
                }
                if(f)break;
                if(j >= m){f = 1;break;}
            }
            if(f)printf("No
    ");
            else printf("Yes
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    UVa 1364
    一个无向图博弈游戏
    poj 2777 Count Color (线段树)
    UVA 1660
    JS中的caller属性
    “给在读研究生+未来要读研同学们的一封受益匪浅的信”(摘录+整合)
    用cmd重命名.htaccess
    java Scoket的c\s结构聊天室
    log4j详解
    检查文本文件编码的Java程序
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139445.html
Copyright © 2011-2022 走看看