0
0
DSA Typescriptprogramming~10 mins

Path Sum Root to Leaf in Binary Tree in DSA Typescript - 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 node value
Return true/false
Subtract node value from sum
Recurse left child
Recurse right child
Return left OR right result
Start from root, check if current node is leaf and sum matches. Otherwise, subtract node value and recurse left and right. Return true if any path matches.
Execution Sample
DSA Typescript
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 there is a root-to-leaf path with the given sum in the binary tree.
Execution Table
StepOperationCurrent NodeSum NeededLeaf?Return ValueVisual State
1Start at root522NoN/A5 / \ 4 8
2Recurse left417 (22-5)NoN/A4 / 11
3Recurse left1113 (17-4)NoN/A11 / 7 2
4Recurse left72 (13-11)YesFalse7
5Recurse right22 (13-11)YesTrue2
6Return from 111113NoTrue11
7Return from 4417NoTrue4
8Recurse right817 (22-5)NoN/A8 / 13 4
9Recurse left139 (17-8)YesFalse13
10Recurse right49 (17-8)YesFalse4
11Return from 8817NoFalse8
12Return from root522NoTrue5
💡 Returned true at leaf node with path sum matching 22
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11Final
Current Node5541172114813485
Sum Needed2222171322131717991722
Return ValueN/AN/AN/AN/AFalseTrueTrueTrueN/AFalseFalseFalseTrue
Key Moments - 3 Insights
Why do we check if the node is a leaf before comparing the sum?
Because only root-to-leaf paths count. The check at Step 4 and 5 ensures we only compare sum when at a leaf node, as shown in execution_table rows 4 and 5.
Why do we subtract the node value from sum before recursion?
We reduce the sum needed by the current node's value to check if the remaining path sums to the reduced sum. This is seen in the 'Sum Needed' column changing each step.
What happens if a path returns true early?
The function returns true immediately without checking other paths, as seen at Step 5 returning true and propagating back up in Steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the sum needed when visiting node 11 at Step 3?
A13
B17
C22
D2
💡 Hint
Check the 'Sum Needed' column at Step 3 in the execution_table.
At which step does the function first return true?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Return Value' column in execution_table rows 4 and 5.
If the left subtree of node 4 was missing, what would be the return value at Step 7?
AN/A
BFalse
CDepends on right subtree
DTrue
💡 Hint
Step 7 return depends on left and right subtree results as per execution_table logic.
Concept Snapshot
Path Sum Root to Leaf in Binary Tree:
- Start at root node
- If node is leaf, check sum == node.val
- Else subtract node.val from sum
- Recurse left and right
- Return true if any path matches sum
- Stops early if path found
Full Transcript
This concept checks if a binary tree has a root-to-leaf path where the sum of node values equals a target sum. We start at the root and check if the node is null, returning false if so. If the node is a leaf, we compare the remaining sum with the node's value. If they match, we return true. Otherwise, we subtract the node's value from the sum and recursively check the left and right children. The function returns true if any path matches the sum. The execution table shows each step, the current node, the sum needed, whether the node is a leaf, and the return value. The variable tracker shows how the current node and sum needed change at each step. Key moments clarify why leaf checks happen first, why sum is reduced, and how early returns work. The visual quiz tests understanding of sum values at steps, when true is returned, and how missing subtrees affect results.