Easy C++ Solution


  • 0
    G
    class Solution {
    public:
        unordered_map<TreeNode*, int> m;
        int findTilt(TreeNode* root) {
            if (!root) return 0;
            int tilt = abs(getSum(root->left, m) - getSum(root->right, m));
            return tilt + findTilt(root->left) + findTilt(root->right);
        }
        int getSum(TreeNode* node, unordered_map<TreeNode*, int>& m) {
            if (!node) return 0;
            if (m.count(node)) return m[node];
            return m[node] = getSum(node->left, m) + getSum(node->right, m) + node->val;
        }
    };

Log in to reply
 

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