JavaScript Solution, Concise ES6 using IIFE


  • 1
    M

    Pretty much the JS equivalent of the top java solution.

    function findTilt(root) {
        let tilt = 0;
        (function postTraversal(root) {
            if (!root) return 0;
            const left = postTraversal(root.left);
            const right = postTraversal(root.right);
            
            tilt += Math.abs(left - right);
            
            return left + right + root.val;
        })(root);
        return tilt;
    }
    

Log in to reply
 

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