0

I am facing a problem related to dfs in C programming. I need to find Count Nodes Equal to Average of Subtree. Unfortunately, I did not find enough guides on c, so I am asking for help. This code still doesn't work and gives wrong results, so I'd appreciate it if you could point out my mistakes.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int ans=0;
int dfs(struct TreeNode* root){
    if (root == NULL) return 0,0;
    int left,left_count = dfs(root->left);
    int right,right_count = dfs(root->right);
    int totalSum = left+right+root->val;
    int count = left_count + right_count + 1;
    if (totalSum/count == root->val) ans++;
    return totalSum,count;
}

int averageOfSubtree(struct TreeNode* root){
    dfs(root);
    return ans;
}

I've modified this code many times, but I've never gotten to a working code. At the output, I get the data, but it is incorrect (thanks in advance).

  • 4
    It looks like you expect your function to return a tuple of two ints, but `0, 0` evaluates to 0, and `totalSum, count` evaluates to just `count`. This is not Python. – trincot Jan 22 '23 at 21:27
  • 1
    i dont quite understand what the ```left``` variable is supposed to hold in your code. what do you want to accomplish with ```int left, left_count = ...```, ```left``` will have garbage value, since its not initialized – yad0 Jan 22 '23 at 21:29
  • 1
    what you're doing in ```totalSum``` is, you are adding two uninitialized variables (```left``` and ```right```), which will have random values to it. therefore ```totalSum``` will also have a random value. also one other thing to note is, you can't return tuples in c – yad0 Jan 22 '23 at 21:34
  • 1
    [Enabling more warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) in your compiler would highlight most of your problems. – pmacfarlane Jan 22 '23 at 21:40

1 Answers1

1

Some issues:

  • You seem to intend that your dfs function returns two int values, but this is not true. Your function is declared as returning int (one), and the comma operator will not return a tuple (like in Python), but will evaluate to the second argument.

    So return 0,0; has the same effect as return 0;

    And return totalSum,count; has the same effect as return count;

    And int left,left_count = dfs(root->left); will not initialise left, only left_count

    It is true that you need both the count and sum to be made available to the caller, and there are several ways you could do that. One is to pass pointers to int variables as arguments to dfs.

  • As ans is global, and is not reset to 0, the tests on Leet Code will accumulate results from previous runs with the current run. It is better to avoid the global variable all together. Instead, you could make it the return value of dfs. Or also a pointer to a variable that is passed as argument...

Corrected version:

int dfs(struct TreeNode* root, int *count, int *sum){
    if (root == NULL) {
        *count = *sum = 0;
        return 0;
    }
    int left, left_count, right, right_count;
    int ans = dfs(root->left, &left_count, &left) + dfs(root->right, &right_count, &right);
    *sum = left + right + root->val;
    *count = left_count + right_count + 1;
    return ans + (*sum/(*count) == root->val);
}

int averageOfSubtree(struct TreeNode* root){
    int sum, count; // Dummy
    return dfs(root, &sum, &count);
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you very much for the excellent explanation, you really helped me a lot because there are really very few guides on c and it is difficult to understand some topics. –  Jan 22 '23 at 22:17