K23

Path Sum to Leaf

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: BOOLEAN

Problem Statement & Objective

Given the root of a binary tree and an integer targetSum, return True if the tree has a root-to-leaf path such that adding up all values equals targetSum.

Function Signature (Python)

has_path_sum(root: list, target: int) -> bool

Input & Output Contract

Output Rule:True veya False.

System Constraints

0 <= agac.length <= 5000
-1000 <= agac[i], hedef <= 1000

Example Execution Walkthrough

agac = [5, 4, 8, 11, None, 13, 4, 7, 2], hedef = 22
5 -> 4 -> 11 -> 2 toplamı 22 -> True

Example Test Scenarios (3)

Example 1: Yol Var
Input:[[5,4,8,11,null,13,4,7,2],22]
Expected Output:true
Example 2: 1+2=3, 1+3=4 (5 yok)
Input:[[1,2,3],5]
Expected Output:false
Example 3: Boş Ağaç
Input:[[],0]
Expected Output:false
Loading Python Editor...
Input Parameters
[
  [
    5,
    4,
    8,
    11,
    null,
    13,
    4,
    7,
    2
  ],
  22
]
Expected Output
true