Simple C solution 9ms (recursion ,no global variable)


  • 0
    X
    int tiltcount(struct TreeNode *root ,int *sum)
    {
        if(!root) { return 0;}
        int L = tiltcount(root->left,sum); // sum of left nodes
        int R= tiltcount(root->right,sum); //sum of right nodes
        *sum += abs(L-R); //absolute difference 
        return (L+R+root->val) ; 
    }
    
    int findTilt(struct TreeNode* root) {
        int sum = 0;
        tiltcount(root,&sum);
        return sum;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.