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日

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线