C 查找bst中所有非终端节点的和

C 查找bst中所有非终端节点的和,c,recursion,struct,binary-search-tree,function-definition,C,Recursion,Struct,Binary Search Tree,Function Definition,如何更正此程序 将输出设置为0。 请帮帮我。 我是编程新手。 我试过输入10,15,9,8 它给我的输出是零或一些大的数字。 我知道这是个愚蠢的问题,但我找不到答案 void findProductSum(struct node* root, int sum) { if (root == NULL || (root->left == NULL&& root->right == NULL)) return; if (root-&

如何更正此程序

将输出设置为0。 请帮帮我。 我是编程新手。 我试过输入10,15,9,8 它给我的输出是零或一些大的数字。 我知道这是个愚蠢的问题,但我找不到答案



void findProductSum(struct node* root, int sum)
{

    if (root == NULL || (root->left == NULL&& root->right == NULL))
        return;


    if (root->left != NULL || root->right != NULL)
    {

        sum += root->data;
        
    }

    findProductSum(root->left,sum);
    findProductSum(root->right,sum);
    return sum;

}```

该函数具有返回类型
void
。比如说这句话

return sum;
if (root->left != NULL || root->right != NULL)
if (root == NULL || (root->left == NULL&& root->right == NULL))
没有道理

这个if语句

return sum;
if (root->left != NULL || root->right != NULL)
if (root == NULL || (root->left == NULL&& root->right == NULL))
这是多余的。您已经检查了此语句中的一个指针是否不等于
NULL

return sum;
if (root->left != NULL || root->right != NULL)
if (root == NULL || (root->left == NULL&& root->right == NULL))
此外,函数中的局部变量
sum
(参数为函数局部变量)也发生了变化

因此,这些电话

findProductSum(root->left,sum);
findProductSum(root->right,sum);
没有效果

该函数可以通过以下方式定义

long long int findProductSum( const struct node* root )
{
    return root == NULL || ( root->left == NULL && root->right == NULL )
           ? 0ll
           : root->data + findProductSum( root->left ) + findProductSum( root->right );
}                            
打电话给我

long long int sum = findProductSum( root );
其中
root
是指向调用者中声明的根节点的指针

然后您可以输出获得的值,如

printf( "%lld\n", sum );
这是一个演示程序

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

struct node
{
    int data;
    struct node *left;
    struct node *right;
};

int insert( struct node **root, int data )
{
    struct node *new_node = malloc( sizeof( struct node ) );
    int success = new_node != NULL;
    
    if ( success )
    {
        new_node->data  = data;
        new_node->left  = NULL;
        new_node->right = NULL;
        
        while ( *root != NULL )
        {
            if ( data < ( *root )->data )
            {
                root = &( *root )->left;
            }
            else
            {
                root = &( *root )->right;
            }
        }
        
        *root = new_node;
    }
    
    return success;
}

long long int findProductSum( const struct node* root )
{
    return root == NULL || ( root->left == NULL && root->right == NULL )
           ? 0ll
           : root->data + findProductSum( root->left ) + findProductSum( root->right );
}  

int main(void) 
{
    struct node *root = NULL;
    int data[] = { 10, 15, 9, 8 };
    const size_t N = sizeof( data ) / sizeof( *data );
    
    for ( size_t i = 0; i < N; i++ ) insert( &root, data[i] );
    
    long long int sum = findProductSum( root );
    
    printf( "The sum of non-terminal nodes is equal to %lld\n", sum );
    
    return 0;
}
值为
15
8
的节点是终端节点。因此,它们的值不会添加到结果和中