0
0
DSA Javascriptprogramming~10 mins

Path Sum Root to Leaf in Binary Tree in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Path Sum Root to Leaf in Binary Tree
Start at root node
Check if node is null?
YesReturn false
No
Check if leaf node?
YesCheck if sum equals target
Return true/false
Recurse left with updated sum
Recurse right with updated sum
Return left_result OR right_result
Start from root, check if node is leaf and sum matches target, else recurse left and right with updated sum, return true if any path matches.
Execution Sample
DSA Javascript
function hasPathSum(node, sum) {
  if (!node) return false;
  if (!node.left && !node.right) return sum === node.val;
  return hasPathSum(node.left, sum - node.val) || hasPathSum(node.right, sum - node.val);
}
Checks if any root-to-leaf path sums to the target value.
Execution Table
StepOperationCurrent NodeSum RemainingReturn ValueVisual Tree State
1Start at root522?5 / \ 4 8 / / \ 11 13 4 / \ 7 2 1
2Check leaf?522NoSame
3Recurse left417?Same
4Check leaf?417NoSame
5Recurse left1113?Same
6Check leaf?1113NoSame
7Recurse left72?Same
8Check leaf?72YesSame
9Compare sum72FalseSame
10Recurse right22?Same
11Check leaf?22YesSame
12Compare sum22TrueSame
13Return true up1113TrueSame
14Return true up417TrueSame
15Return true up522TrueSame
16StopN/AN/ATrueFinal: Path 5->4->11->2 sums to 22
💡 Found a root-to-leaf path with sum 22, so returns true.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 10Final
node541172N/A
sum_remaining22171322N/A
return_value?????True
Key Moments - 3 Insights
Why do we check if the node is a leaf before comparing sums?
Because only root-to-leaf paths count. The check at step 8 and 11 ensures we only compare sums when at a leaf node, as shown in execution_table rows 8-12.
Why do we subtract node.val from sum when recursing?
We reduce the target sum by the current node's value to track how much is left to reach the target. This is seen in the sum_remaining column updating at steps 3, 5, 7, and 10.
What happens if the node is null?
The function returns false immediately (step 2), meaning no path exists through a null node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 11, what is the sum_remaining value?
A13
B17
C2
D22
💡 Hint
Check the 'Sum Remaining' column at step 11 in the execution_table.
At which step does the function find a valid path sum and return true?
AStep 12
BStep 14
CStep 9
DStep 16
💡 Hint
Look for the first 'Compare sum' operation returning true in the execution_table.
If the target sum was 26 instead of 22, what would happen at step 12?
AReturn true because sum matches
BReturn false because sum does not match
CRecurse further left
DReturn false because node is null
💡 Hint
At step 12, the sum comparison depends on sum_remaining matching node.val exactly.
Concept Snapshot
function hasPathSum(node, sum)
- Return false if node is null
- If leaf node, check sum === node.val
- Else recurse left and right with sum - node.val
- Return true if any path matches
- Finds if root-to-leaf path sums to target
Full Transcript
This visualization shows how the path sum check works in a binary tree. Starting at the root, the function checks if the node is null, returning false if so. If the node is a leaf, it compares the remaining sum to the node's value. If they match, it returns true. Otherwise, it recurses down left and right children, subtracting the current node's value from the sum. The execution table traces each step, showing the current node, remaining sum, and return values. The variable tracker shows how node and sum_remaining change. Key moments clarify why leaf checks and sum subtraction are needed. The quiz tests understanding of sum tracking and return points. This method efficiently finds if any root-to-leaf path equals the target sum.