m0_62746615 2022-11-28 09:37 采纳率: 85%
浏览 4
已结题

C/C++程序问题,怎样修改

img


#include<stdio.h>
using namespace std;
struct node {
    int data;
    node* firstChild;
    node* nextBrother;
};
node* CreateTree() 
{
    int k;
    scanf("%d", &k);
    if (k == 0) return NULL;
    node *t = new node;
    t->data = k;
    t->firstChild = CreateTree();
    t->nextBrother = CreateTree();
    return t;
}
void Del(node* root)
{
    if (root == NULL) return;
    node* p = root->firstChild;
    node* next;
    while (p != NULL){
        next = p->nextBrother;
        Del(p);
        p = next;
    }
    delete root;
}

int LeastCommonAncestors(node *root, int a, int b){
if(root==NULL)
return -1;
if(root->data==a||root->data==b)
return root->data;
int fC=LeastCommonAncestors(root->firstChild, a, b);
int nB=LeastCommonAncestors(root->nextBrother, a, b);
if(fC==-1){
    return nB;
}
if(nB==-1){
    return fC;
}
return root->data;

}

int main() 
{
    int a, b, T;
    scanf("%d", &T);
    while(T--){
        node *root = CreateTree();
        scanf("%d %d", &a, &b);
        printf("%d\n", LeastCommonAncestors(root, a, b));
        Del(root);
    }
    return 0;
}

不知道要怎么修改
输入
2
8 5 1 0 6 0 2 0 0 3 4 0 0 7 0 0 0
1 2
8 5 1 0 6 0 2 0 0 3 4 0 0 7 0 0 0
2 4
输出
5
8

  • 写回答

4条回答 默认 最新

  • CSDN专家-link 2022-11-28 09:49
    关注

    现在是什么现象?有测试用例没有

    #include <iostream>
    using namespace std;
    struct node {
        int data;
        node* firstChild;
        node* nextBrother;
    };
    node* CreateTree() 
    {
        int k;
        cin>>k;
        if (k == 0) 
            return NULL;
        node *t = new node;
        t->data = k;
        t->firstChild = CreateTree();
        t->nextBrother = CreateTree();
        return t;
    }
    void Del(node* root)
    {
        if (root == NULL) return;
        node* p = root->firstChild;
        node* next;
        while (p != NULL){
            next = p->nextBrother;
            Del(p);
            p = next;
        }
        delete root;
    }
    
    int Find(node *root ,int n,int r = 1)
    {
        if(root == NULL)
            return -1;
        if(root->data == n)
            return 1;
        if(Find(root->firstChild,n,0) == 1)
            return 1;
        if(r==0)
            return Find(root->nextBrother,n,0);
        return -1;
    }
    
    int LeastCommonAncestors(node *root, int a, int b)
    {
        if(root == NULL)
            return -1;
        if(LeastCommonAncestors(root->firstChild,a,b) != -1)
            return root->firstChild->data;
        if(LeastCommonAncestors(root->nextBrother,a,b) != -1)
            return root->nextBrother->data;
        if(Find(root,a) == 1 && Find(root,b) == 1)
            return root->data;
        return -1;
    }
    
    
    int main() 
    {
        int a, b, T;
        scanf("%d", &T);
        while(T--)
        {
            node *root = CreateTree();
            scanf("%d %d", &a, &b);
            printf("%d\n", LeastCommonAncestors(root, a, b));
            Del(root);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月28日
  • 已采纳回答 11月28日
  • 修改了问题 11月28日
  • 创建了问题 11月28日

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线