C++ Simple Solution, O(n) Time


  • 0
    class Solution {
        
        int dfs(TreeNode* cur) {
            if (cur == nullptr)
                return 0;
            int l = dfs(cur->left), r = dfs(cur->right);
            res += abs(l - r);
            return l + r + cur->val;
        }
        int res = 0;
    public:
        int findTilt(TreeNode* root) {
            dfs(root);
            return res;
        }
    };
    

Log in to reply
 

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