[C++] Clean Code


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

Log in to reply
 

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