Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions exercises/1000_programs/medium/1080_insufficient_nodes_path_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def hasPathSum(root, targetSum):
"""
Determine if tree has root-to-leaf path with target sum.
This logic is fundamental for Decision Trees in Machine Learning.
"""
# Base Case: Agar tree khali hai
if not root:
return False

# Check if we are at a leaf node (node with no children)
if not root.left and not root.right:
# Check if the remaining sum matches the current node's value
return root.val == targetSum

# Recursive step: Subtract current node's value from targetSum
# and search in children subtrees
remaining_sum = targetSum - root.val

return hasPathSum(root.left, remaining_sum) or hasPathSum(root.right, remaining_sum)