Path Sum Root to Leaf in Binary Tree in DSA Typescript - Time & Space Complexity
We want to know how long it takes to check if a path from the root to any leaf adds up to a target sum in a binary tree.
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (!root) return false;
if (!root.left && !root.right) return root.val === targetSum;
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
This code checks if there is any root-to-leaf path where the sum of node values equals the target sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Each node is visited once in the worst case.
As the number of nodes grows, the function checks each node once to find paths.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 node visits |
| 100 | About 100 node visits |
| 1000 | About 1000 node visits |
Pattern observation: The operations grow roughly in direct proportion to the number of nodes.
Time Complexity: O(n)
This means the time needed grows linearly with the number of nodes in the tree.
[X] Wrong: "The function only checks one path, so it runs in constant time."
[OK] Correct: The function explores all possible paths to leaves, so it must visit every node in the worst case.
Understanding this helps you explain how recursive tree traversal works and how to analyze its cost, a common skill in interviews.
"What if the tree was a linked list (each node has only one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem statement
The problem asks if there is any path from the root node down to any leaf node where the sum of the node values equals the target sum.Step 2: Differentiate from other tree properties
It is not about depth, total nodes, or sum of all nodes, but specifically about a path sum from root to leaf.Final Answer:
If there exists a root-to-leaf path with a sum equal to a given target -> Option BQuick Check:
Path sum means root-to-leaf sum = D [OK]
- Confusing path sum with total tree sum
- Checking any path, not just root-to-leaf
- Ignoring leaf node condition
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; }?Solution
Step 1: Check parameter types
The root should be a TreeNode or null (empty tree), and sum should be a number representing the target sum.Step 2: Check return type
The function returns a boolean indicating if such a path exists.Final Answer:
function hasPathSum(root: TreeNode | null, sum: number): boolean -> Option CQuick Check:
Correct types and return boolean = B [OK]
- Swapping parameter types
- Using wrong return type
- Missing null for root parameter
hasPathSum(root, 22) return?5 / \ 4 8 / / \ 11 13 4 / \ 7 2 1
Solution
Step 1: Identify root-to-leaf paths and their sums
Check paths: 5->4->11->7 = 27, 5->4->11->2 = 22, 5->8->13 = 26, 5->8->4->1 = 18.Step 2: Check if any path sum equals 22
Path 5->4->11->2 sums to 22, so function returns true.Final Answer:
true -> Option DQuick Check:
Path sum 22 exists = true [OK]
- Ignoring leaf node condition
- Calculating sum incorrectly
- Confusing partial paths with full root-to-leaf
function hasPathSum(root: TreeNode | null, sum: number): boolean {
if (!root) return false;
if (!root.left && !root.right) return root.val === sum;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}What does the function return if the tree is empty (root = null) and sum is 0?
Solution
Step 1: Analyze base case for empty tree
When root is null, if (!root) returns false immediately.Step 2: Confirm for sum = 0
Empty tree has no root-to-leaf path regardless of target sum, so false is correct.Final Answer:
Returns false for empty tree and sum 0 -> Option AQuick Check:
Null root always returns false [OK]
- Thinking empty tree with sum 0 should return true
- Expecting runtime error without null check
- Confusing empty tree with single-node tree
Solution
Step 1: Understand requirement to collect all matching paths
We need to find all root-to-leaf paths whose sums equal the target, not just check existence.Step 2: Choose traversal and tracking method
DFS (depth-first search) allows tracking the current path and sum, and collecting paths when leaf and sum match.Step 3: Evaluate other options
BFS is less natural for path tracking; queues without paths lose information; returning boolean stops early.Final Answer:
Use DFS to track current path and sum, collect paths when sum matches at leaf -> Option AQuick Check:
DFS with path tracking = A [OK]
- Using BFS without path tracking
- Stopping at first match instead of collecting all
- Ignoring leaf node condition
Ready to prove it?
Pass the test and this pattern moves unknown
