计算节点值的最大和,并计算给出和的特定路径[C]

计算节点值的最大和,并计算给出和的特定路径[C],c,count,tree,structure,C,Count,Tree,Structure,好了,伙计们,我有一个有趣的问题。给我的任务是计算给定树从根到叶的最大和。在这种情况下是14。问题是,我还需要计算路径的长度,并返回它,因为我需要将总和除以路径长度。这听起来很复杂,起初我认为这很简单,但后来我找不到通过特定路径计算节点的方法。可能整个函数count()组装错误,因为它没有为我需要完成的特定任务留出空间。如果有更多的问题,请随时写下来,我需要这个答案。谢谢 #include <stdio.h> #include <stdlib.h> struct tre

好了,伙计们,我有一个有趣的问题。给我的任务是计算给定树从根到叶的最大和。在这种情况下是14。问题是,我还需要计算路径的长度,并返回它,因为我需要将总和除以路径长度。这听起来很复杂,起初我认为这很简单,但后来我找不到通过特定路径计算节点的方法。可能整个函数
count()
组装错误,因为它没有为我需要完成的特定任务留出空间。如果有更多的问题,请随时写下来,我需要这个答案。谢谢

#include <stdio.h>
#include <stdlib.h>

struct tree{
    int i;
    struct tree *left;
    struct tree *right;
};
int count(struct tree *root);
int max(int,int);
int main()
{
    struct tree *p=NULL, *q=NULL, *r=NULL, *t=NULL;

    //1
    p=(struct tree *)malloc(sizeof(struct tree));
    if(p==NULL) exit(1);
    p->i=1;
    p->left=NULL;
    p->right=NULL;

    //2
    q=(struct tree *)malloc(sizeof(struct tree));
    if(q==NULL) exit(1);
    q->i=2;
    q->left=NULL;
    q->right=NULL;
    p->left=q;

    //3
    r=(struct tree *)malloc(sizeof(struct tree));
    if(r==NULL) exit(1);
    r->i=3;
    r->left=NULL;
    r->right=NULL;
    p->right=r;

    t=q;

    //4
    q=(struct tree *)malloc(sizeof(struct tree));
    if(q==NULL) exit(1);
    q->i=4;
    q->left=NULL;
    q->right=NULL;
    t->left=q;

    //5
    q=(struct tree *)malloc(sizeof(struct tree));
    if(q==NULL) exit(1);
    q->i=5;
    q->left=NULL;
    q->right=NULL;
    t->right=q;

    t=q;

    //6
    q=(struct tree *)malloc(sizeof(struct tree));
    if(q==NULL) exit(1);
    q->i=6;
    q->left=NULL;
    q->right=NULL;
    t->left=q;

    //7
    q=(struct tree *)malloc(sizeof(struct tree));
    if(q==NULL) exit(1);
    q->i=7;
    q->left=NULL;
    q->right=NULL;
    r->right=q;
    printf("The sum is %d!",count(p));
}
int count(struct tree *root){
    if(root->left!=NULL && root->right!=NULL){
        return root->i+max(count(root->left),count(root->right));
    }
    else if(root->left==NULL && root->right!=NULL){
        return root->i+count(root->right);
    }
    else if(root->left!=NULL && root->right==NULL){
        return root->i+count(root->left);
    }
    else{
        return root->i;
    }
}
int max(int a, int b){
    if(a>b){
        return a;
    }
    else{
        return b;
    }
}
#包括
#包括
结构树{
int i;
结构树*左;
结构树*右;
};
整数计数(结构树*根);
int max(int,int);
int main()
{
结构树*p=NULL,*q=NULL,*r=NULL,*t=NULL;
//1
p=(结构树*)malloc(sizeof(结构树));
如果(p==NULL)退出(1);
p->i=1;
p->left=NULL;
p->right=NULL;
//2
q=(结构树*)malloc(sizeof(结构树));
如果(q==NULL)退出(1);
q->i=2;
q->left=NULL;
q->right=NULL;
p->left=q;
//3
r=(结构树*)malloc(sizeof(结构树));
如果(r==NULL)退出(1);
r->i=3;
r->left=NULL;
r->right=NULL;
p->right=r;
t=q;
//4
q=(结构树*)malloc(sizeof(结构树));
如果(q==NULL)退出(1);
q->i=4;
q->left=NULL;
q->right=NULL;
t->left=q;
//5
q=(结构树*)malloc(sizeof(结构树));
如果(q==NULL)退出(1);
q->i=5;
q->left=NULL;
q->right=NULL;
t->right=q;
t=q;
//6
q=(结构树*)malloc(sizeof(结构树));
如果(q==NULL)退出(1);
q->i=6;
q->left=NULL;
q->right=NULL;
t->left=q;
//7
q=(结构树*)malloc(sizeof(结构树));
如果(q==NULL)退出(1);
q->i=7;
q->left=NULL;
q->right=NULL;
r->right=q;
printf(“总和是%d!”,计数(p));
}
整数计数(结构树*根){
if(root->left!=NULL&&root->right!=NULL){
返回root->i+max(count(root->left)、count(root->right));
}
else if(root->left==NULL&&root->right!=NULL){
返回root->i+计数(root->right);
}
else if(root->left!=NULL&&root->right==NULL){
返回root->i+计数(root->left);
}
否则{
返回root->i;
}
}
最大整数(整数a,整数b){
如果(a>b){
返回a;
}
否则{
返回b;
}
}

我不想改变一切,只想调整你的代码

typedef struct {
  int len;
  int sum;
} path_t;

path_t count(struct tree *root) {
    path_t a = { .len = 0, .sum = 0};
    path_t b = { .len = 0, .sum = 0};
    path_t ret;

    if (root == NULL)
      return a;  // empty sub-tree

    a = count(root->left);
    b = count(root->right);

    if (a.sum > b.sum) {
      ret.sum = a.sum:
      ret.len = a.len;
    } else {
      ret.sum = b.sum:
      ret.len = b.len;
    }
    ret.sum += root->i;
    ret.len ++;
    return ret;
}
您可能需要注意的一些细节: 如果树可以包含负数,则将.sum初始化为0可能不够。
对于
a.sum==b.sum
我只需要
b
。您可能希望根据路径的长度来决定,或者总是选择
a
而不是
b
,“这听起来很复杂”——听起来既不像,也不是。然而,我们没有“做我的作业”网站。看见你的具体问题是什么?你试过什么?我试过所有的方法,但都不管用,我明白为什么不管用——我找不到解决办法。问题很简单-此函数为我提供了从根到叶的最大和,但我无法计算如何计算这些特定节点。为什么不将另一个参数
int*pathlen
传递到
count()
,该参数保存此(子)路径的长度?当你从树枝上走下来,走到左边时,你把它设为1。当你向上爬的时候,你从小径上取下len并增加1。这听起来像是我做的事情,但它给了我树的长度?@Slavkovačević你说的“树的长度”是什么意思?节点总数或最长路径?如果对同一子树使用正确的路径作为最大和,则应获得正确的长度。你应该试一试。