JavaScript DFS Clean Solution


  • 0
    var hasPathSum = function(root, sum) {
        if(!root) return false;
        var result = false;
        function add(root, amount){
            amount += root.val;
            if(!root.left && !root.right){
                if(amount === sum) result = true;
            }
            if(root.left) add(root.left, amount);
            if(root.right) add(root.right, amount);
        }
        add(root, 0);
        return result;
    };

Log in to reply
 

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