peacern 2022-04-25 14:00 采纳率: 100%
浏览 26
已结题

二叉树搜索 C++ 🧐66

img


这该怎么做?给点思路,解法
(判断若干个序列是否为同一个二叉树序列)

  • 写回答

1条回答 默认 最新

  • bostonAlen 2022-04-26 17:19
    关注
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    struct node {
        node *left;
        node *right;
        int num;
    }tree[105];                //静态数组
    
    char str1[30], str2[30];//x表示原始的中后序编历,y表示后来的中后序编历
    int cnt;            //静态数组中要使用的元素
    int num;            //字符数组中要使用的元素
    
    node *creat()        //申请新结点
    {
        tree[cnt].left = tree[cnt].right = NULL;
        return &tree[cnt++];
    }
    
    node *build(int x, node*t)            
    {
        if (t == NULL)
        {
            t = creat();
            t->num = x;
        }
        else if (x < t->num) 
        {
            t->left = build(x, t->left);
        }
        else if (x > t->num) 
        {
            t->right = build(x, t->right);
        }
        return t;
    }
    
    void in_order(node *root) {//中序遍历
        if (root == NULL) return;
        in_order(root->left);
        str2[num++] = root->num + '0';
        in_order(root->right);
    }
    
    void port_order(node *root)            //后序编历
    {
        if (root == NULL)return;
        port_order(root ->left);
        port_order(root->right);
        str2[num++] = root->num + '0';
    }
    int main()
    {
        int n;
        while (cin>> n&&n!=0)
        {
            scanf("%s", str1);                        
            int len = strlen(str1);                //用strlen求x数组的长度
            cnt = 0; num = 0;
            node *t = NULL;
            for (int i = 0; i < len; i++)        //建立最开始的二叉排序树即二叉搜索树
            {
                t = build(str1[i] - '0',t);
            }
            in_order(t);
            port_order(t);                    //开始编历
            for (int i = 0; i < num; i++)
            {
                str1[i] = str2[i];
            }
            while (n--)                            //循环至输入n之后结束
            {
                cnt = 0; num = 0;
                cin>>str2;
                int len = strlen(str2);
                node *tt = NULL;
                for (int i = 0; i < len; i++)    //建立用于比较的二叉搜索树
                {
                    tt = build(str2[i] - '0', tt);
                }
                in_order(tt);                    //进行编历
                port_order(tt);
                int i;
                for (i = 0; i < num; i++)
                {
                    if (str1[i] != str2[i])
                    {
                        break;
                    }
                }
                if (i == num)
                {
                    cout << "YES" << endl;                                    //可输出比较结果进行判断是否正确
                    /*cout << "编历结果如下:" << endl;
                    cout << "原先的中后序编历结果:";
                    for (int i = 0; i < num; i++)
                        cout << str1[i];
                    cout << endl;
                    cout << "被比较的序列中后序编历结果:";
                    for (int i = 0; i < num; i++)
                        cout << str2[i];*/
                }
                else cout << "NO" << endl;
            }
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 创建了问题 4月25日

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能