C++ Solution, Single Function, without Global Variable


  • 0
    H
    class Solution {
    public:
        int findTilt(TreeNode* root) {
            if(!root) return 0;
            int childTilts = findTilt(root->left) + findTilt(root->right);
            int tilt = 0;
            if(root->left) {
                root->val += root->left->val;
                tilt = root->left->val;
            }
            if(root->right) {
                root->val += root->right->val;
                tilt -= root->right->val;
            }
            return childTilts + abs(tilt);
        }
    };
    

Log in to reply
 

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