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)
}Discussion 0
Join free to post in the discussion.
Related questions
- What a 504 from Nginx meansBackend Developer
- Python default list surpriseBackend Developer
- Edges in a tree with n nodesBackend Developer
- Most nodes in a binary tree of height 3Backend Developer
- What inorder traversal of a BST givesBackend Developer
- Preorder output of a small treeBackend Developer
- Traversal for freeing a whole treeBackend Developer
- Data structure for level-order traversalBackend Developer