03-树1 树的同构分数 25

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。

图1

                                   

图2

现给定两棵树,请你判断它们是否是同构的。

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:

如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):

8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -

输出样例1:

Yes

输入样例2(对应图2):

8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:

No

思路分析:

两棵树的左右孩子数字可能刚好相反,因此我们可以先将输入的二叉树格式化:①若左右只有一个孩子,则均将孩子置于左侧。②若左右均有两个孩子则可以将左侧放置value更小的,右侧放置更大的。

void sorted(node root)                         //将树结构格式化
{                                                
    queue<node> treenode;                        //运用队列来处理所有节点
    treenode.push(root);
    while (!treenode.empty())
    {
        node point = treenode.front();
        treenode.pop();
        node l = point->lch, r = point->rch;
        if (l || r)                                //左右节点不均为空
        {
            if (l && r)                               //左右节点均非空
            {
                treenode.push(l);                    //加入队列继续继续格式化
                treenode.push(r);
                if (l->data > r->data)                //保证左节点比右节点小
                {
                    node p = point->lch;
                    point->lch = point->rch;
                    point->rch = p;
                }
            }
            else                                    //左右节点有一个为空,保证左节点不空
            {
                if (l < r)
                {
                    treenode.push(r);
                    point->lch = point->rch;
                    point->rch = NULL;
                }
                else
                {
                    treenode.push(l);
                }
            }

        }
    }
}

 将树的结构格式化后再进行比较则简单许多:

运用按层遍历的方法来解决:

bool compare(node root1, node root2)            //比较函数
{
    sorted(root1);
    sorted(root2);
    queue<node> tree1, tree2;                            //继续加入队列依次比较
    tree1.push(root1), tree2.push(root2);
    while ((!tree1.empty()) && (!tree2.empty()))            //队列均不空继续比较
    {
        node p = tree1.front(), q = tree2.front();            //取第一个节点比较
        tree1.pop(), tree2.pop();
        if (p->data != q->data)
        {
            return false;
        }
        if (p->lch)                                        //左节点非空
        {
            if (q->lch)
            {
                tree1.push(p->lch);
                tree2.push(q->lch);
            }
            else
            {
                return false;
            }
        }
        else if (q->lch)                                
        {
            return false;
        }
        if (p->rch)                                        //右节点非空
        {
            if (q->rch)
            {
                tree1.push(p->rch);
                tree2.push(q->rch);
            }
            else
            {
                return false;
            }
        }
        else if (q->rch)
        {
            return false;
        }

    }
    if ((!tree1.empty()) || (!tree2.empty())) //一个队列排完,另一个队列还存在元素,一定不同
    {
        return false;
    }
    return true;
}

完整代码:

#include<iostream>
#include<queue>
using namespace std;
typedef struct tree        //创建二叉树
{ 
    tree* lch;
    tree* rch;
    char data;
}*node;

int input(int n);
node createtree(int n, node root);
void sorted(node root);
bool compare(node root1, node root2);

node root1, root2;
char** arry;
int input(int n)
{
    arry = new char* [n];
    int* a = new int[n];                
    for (int i = 0; i < n; i++)
    {
        a[i] = 0;
    }
    for (int i = 0; i < n; i++)            //以二维数组保存输入数据
    {
        char* po = new char[3];
        cin >> po[0] >> po[1] >> po[2];
        arry[i] = po;    
        a[po[1] - '0']++;                    //出现过,则不是根节点
        a[po[2] - '0']++;            
    }
    for (int i = 0; i < n; i++)            //寻找根节点
    {
        if (a[i] == 0)
        {
            return i;
        }
    }
    return -1;                        
}

node createtree(int i, node root)                //开始建造二叉树
{
    root = new tree;
    root->lch = NULL;
    root->rch = NULL;
    root->data = arry[i][0];                    
    if (arry[i][1] != '-')
    {
        root->lch=createtree(int(arry[i][1] - '0'), root->lch);       //左递归建树
    }
    if (arry[i][2] != '-')
    {
        root->rch=createtree(int(arry[i][2] - '0'), root->rch);        //右递归建树
    }    
    return root;
}

void sorted(node root)                         //将树结构格式化
{                                                
    queue<node> treenode;                        //运用队列来处理所有节点
    treenode.push(root);
    while (!treenode.empty())
    {
        node point = treenode.front();
        treenode.pop();
        node l = point->lch, r = point->rch;
        if (l || r)                                //左右节点不均为空
        {
            if (l && r)                               //左右节点均非空
            {
                treenode.push(l);                    //加入队列继续继续格式化
                treenode.push(r);
                if (l->data > r->data)                //保证左节点比右节点小
                {
                    node p = point->lch;
                    point->lch = point->rch;
                    point->rch = p;
                }
            }
            else                                    //左右节点有一个为空,保证左节点不空
            {
                if (l < r)
                {
                    treenode.push(r);
                    point->lch = point->rch;
                    point->rch = NULL;
                }
                else
                {
                    treenode.push(l);
                }
            }

        }
    }
}

bool compare(node root1, node root2)            //比较函数
{
    sorted(root1);
    sorted(root2);
    queue<node> tree1, tree2;                            //继续加入队列依次比较
    tree1.push(root1), tree2.push(root2);
    while ((!tree1.empty()) && (!tree2.empty()))            //队列均不空继续比较
    {
        node p = tree1.front(), q = tree2.front();            //取第一个节点比较
        tree1.pop(), tree2.pop();
        if (p->data != q->data)
        {
            return false;
        }
        if (p->lch)                                        //左节点非空
        {
            if (q->lch)
            {
                tree1.push(p->lch);
                tree2.push(q->lch);
            }
            else
            {
                return false;
            }
        }
        else if (q->lch)                                
        {
            return false;
        }
        if (p->rch)                                        //右节点非空
        {
            if (q->rch)
            {
                tree1.push(p->rch);
                tree2.push(q->rch);
            }
            else
            {
                return false;
            }
        }
        else if (q->rch)
        {
            return false;
        }

    }
    if ((!tree1.empty()) || (!tree2.empty())) //一个队列排完,另一个队列还存在元素,一定不同
    {
        return false;
    }
    return true;
}
int main()
{
    int n, m;
    cin >> n;
    if(n!=0)
    {
    int P = input(n);
    root1 = createtree(P, root1);
    }
    cin >> m;
    if(n!=m)                        //两节点数不同直接返回
    {
        cout<<"No";
        return 0;
    }
    if(m!=0)
    {
    int Q = input(m);
    root2 = createtree(Q, root2);
    }
    if(n==0&&m==0)                    
    {
        cout<<"Yes";
        return 0;
    }
    if (compare(root1, root2))
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值