Categories
Binary Tree Depth First Search Leetcode Recursion

Path Sum I

let hasPathSum = function(root, sum) {

if (!root) return false;
if (sum - root.val === 0 && !root.left && !root.right) return true;

return hasPathSum(root.left, sum - root.val)
|| hasPathSum(root.right, sum - root.val);
};

Leave a Reply

Your email address will not be published. Required fields are marked *