JavaScript Recursion


  • 0
    W

    Simply DFS down the tree, and only check the value at the leaf :)

    function hasPathSum(root, sum, current) {
        
        if (!root) return false;
        
        current = current || 0;
        current += root.val;
        
        if (!root.left && !root.right) {
            return sum == current;
        }
        
        return hasPathSum(root.left, sum, current) || hasPathSum(root.right, sum, current);
      
    };
    

Log in to reply
 

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