Path Sum Root to Leaf in Binary Tree in DSA Java - 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.
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
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 if there is a root-to-leaf path where the sum of node values equals targetSum.
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 the path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls |
| 100 | About 100 recursive calls |
| 1000 | About 1000 recursive calls |
Pattern observation: The work grows roughly in direct proportion to the number of nodes.
Time Complexity: O(n)
This means the time to check the path sum 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 by visiting every node until it finds a valid path or finishes checking all.
Understanding how recursive tree traversal scales helps you explain your approach clearly and shows you grasp fundamental algorithm analysis.
"What if the tree is balanced versus very skewed? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the problem statement
The problem asks if any path from the root node down to any leaf node sums exactly to a given target value.Step 2: Identify what is being checked
We check if such a path exists, not the maximum depth or total sum of all nodes.Final Answer:
If there exists a root-to-leaf path with a sum equal to a given target -> Option AQuick Check:
Path sum check = existence of target sum path [OK]
- Confusing total tree sum with path sum
- Checking any path, not root-to-leaf
- Looking for maximum depth instead
Solution
Step 1: Identify the return type for path sum check
The function should return true or false, so the return type must be boolean.Step 2: Check method signature correctness
public boolean hasPathSum(TreeNode root, int targetSum) returns boolean and takes root node and target sum as parameters, matching the problem requirements.Final Answer:
public boolean hasPathSum(TreeNode root, int targetSum) -> Option DQuick Check:
Return type boolean for true/false check [OK]
- Using int or void return type instead of boolean
- Returning String instead of boolean
- Incorrect parameter types
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
if (root.left == null && root.right == null) return root.val == targetSum;
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(4);
root.right = new TreeNode(8);
root.left.left = new TreeNode(11);
root.left.left.left = new TreeNode(7);
root.left.left.right = new TreeNode(2);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(4);
root.right.right.right = new TreeNode(1);
Solution sol = new Solution();
System.out.println(sol.hasPathSum(root, 22));
}
}Solution
Step 1: Trace the path sums in the tree
Check if any root-to-leaf path sums to 22. One path is 5->4->11->2 = 22.Step 2: Evaluate the recursive calls
The function returns true because the path sum 22 exists, so it prints true.Final Answer:
true -> Option AQuick Check:
Path 5+4+11+2 = 22 exists [OK]
- Ignoring leaf node condition
- Subtracting sum incorrectly
- Assuming sum must be from root to any node
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
if (root.left == null && root.right == null) return root.val == targetSum;
return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
}Solution
Step 1: Analyze recursive call parameters
The code calls hasPathSum with the same targetSum without subtracting root.val, so it doesn't reduce the sum along the path.Step 2: Correct the recursive calls
Subtract root.val from targetSum in recursive calls to track remaining sum needed.Final Answer:
Subtract root.val from targetSum in recursive calls -> Option BQuick Check:
Update targetSum by subtracting node value [OK]
- Not subtracting node value in recursion
- Returning true on null node
- Ignoring right subtree
Solution
Step 1: Understand impact of negative values on path sum
Negative values do not affect the logic; the sum is still accumulated along the path.Step 2: Confirm standard recursive approach works
The standard recursive subtraction and leaf check works regardless of node values being negative, zero, or positive.Final Answer:
No modification needed; standard path sum check works for all values -> Option CQuick Check:
Standard method handles all integer values [OK]
- Thinking negative values break the logic
- Trying to skip negative nodes
- Changing leaf node condition unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
