Skip to content
RED SAGSkills
medium10 pts · first answer countsgraphsbinary-treesdebugging

Bug in a root-to-leaf path sum

This should return true only if some root-to-leaf path adds up to target. For a root 1 with only a right child 2 and target 1, what does it return, and why?

function hasPathSum(n, target) {
  if (!n) return target === 0
  return hasPathSum(n.left, target - n.val) ||
         hasPathSum(n.right, target - n.val)
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions