Could anybody help me with this weird compile error?


  • 0

    I got a compile error with no error message for my code. It turns out that the error can be fixed if I add a line to calculate param instead of directly passing the expression as a parameter. I am super confused why this happened; could anyone help me?

    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            helper(root, 0);
            return root;
        }
        
        int helper(TreeNode* root, int sum) {
            if (root == nullptr) {
                return 0;
            }
            int rightSum = helper(root->right, sum);
    
            // the error dissappears after adding the following line 
            // and pass param instead of the right hand side expression
            int param = root->val + rightSum + sum;
    
            // this was originally 
            // int leftSum = helper(root->left, root->val + rightSum + sum);
            int leftSum = helper(root->left, param);
    
            int orig = root->val;
            
            root->val = root->val + rightSum + sum;
    
            return leftSum + rightSum + orig;
        }
    };
    

Log in to reply
 

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