Path Sum Root to Leaf in Binary Tree in DSA C - Time & Space Complexity
We want to know how the time to check if a path sum exists grows as the tree gets bigger.
How does the number of nodes affect the work done?
Analyze the time complexity of the following code snippet.
// Check if any root-to-leaf path sums to target
int hasPathSum(struct TreeNode* root, int targetSum) {
if (root == NULL) return 0;
if (root->left == NULL && root->right == NULL) {
return (targetSum == root->val);
}
return hasPathSum(root->left, targetSum - root->val) ||
hasPathSum(root->right, targetSum - root->val);
}
This code checks every path from the root to leaves to find if any path sums to the target.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
As the number of nodes grows, the function visits each node once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The work grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to check 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 must check all root-to-leaf paths, so it visits every node, making the time grow with tree size.
Understanding this helps you explain how recursive tree problems scale and shows you can analyze traversal costs clearly.
"What if the tree is balanced vs completely skewed? 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 a given target sum.Step 2: Differentiate from other tree properties
This is different from finding max depth, counting leaves, or summing all nodes. It specifically checks for a path with a certain sum.Final Answer:
If there exists a root-to-leaf path with a sum equal to a given target -> Option AQuick Check:
Path sum means root-to-leaf sum check [OK]
- Confusing path sum with total tree sum
- Checking any path, not root-to-leaf
- Counting nodes instead of summing values
Solution
Step 1: Identify correct parameter types
The function needs the root pointer of the tree (struct TreeNode*) and the target sum (int).Step 2: Identify correct return type
The function returns an int (usually 0 or 1) indicating false or true for path existence.Final Answer:
int hasPathSum(struct TreeNode* root, int sum); -> Option AQuick Check:
Function returns int and takes root pointer and sum [OK]
- Using void return type instead of int
- Passing root as int instead of pointer
- Missing sum parameter
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
Solution
Step 1: Find root-to-leaf paths and their sums
Paths and sums: - 5->4->11->7 = 27 - 5->4->11->2 = 22 - 5->8->13 = 26 - 5->8->4->1 = 18Step 2: Check if any path sum equals 22
The path 5->4->11->2 sums to 22, so the function returns true (1).Final Answer:
1 -> Option BQuick Check:
Path sum 22 exists = 1 [OK]
- Ignoring leaf node condition
- Stopping at non-leaf nodes
- Miscomputing sums
int hasPathSum(struct TreeNode* root, int sum) {
if (!root) return 0;
if (!root->left && !root->right && root->val == sum) return 1;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
What is the bug in this code?Solution
Step 1: Analyze base cases
Returns 0 if root is NULL (empty tree). Returns 1 if leaf node value equals sum.Step 2: Analyze recursive calls
Recursively checks left and right subtrees with updated sum (sum - root->val).Final Answer:
It correctly implements the path sum check -> Option DQuick Check:
Code matches standard path sum logic [OK]
- Missing leaf node check
- Not subtracting root value from sum
- Returning wrong values for NULL nodes
Solution
Step 1: Understand path tracking during recursion
To record the current path from root to current node, a stack or dynamic array is used to push and pop nodes as recursion goes deeper or backtracks.Step 2: Differentiate from other data structures
A queue is for breadth-first traversal, hash map is for counting, linked list for storing nodes but not efficient for backtracking paths.Final Answer:
A stack or dynamic array to track nodes in current path -> Option CQuick Check:
Use stack/array for path tracking [OK]
- Using queue instead of stack for path tracking
- Not backtracking path correctly
- Using hash map for path storage
Ready to prove it?
Pass the test and this pattern moves unknown
