0
0
DSA Typescriptprogramming~10 mins

Lowest Common Ancestor in Binary Tree in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Lowest Common Ancestor in Binary Tree
Start at root node
Check if root is null or matches p or q
Yes
Return root as LCA candidate
No
Recurse left subtree
Recurse right subtree
If both left and right recursion return non-null
Current node is LCA
Return non-null child from left or right
End
Start from root, check if it matches either node. Recurse left and right. If both sides find nodes, current is LCA. Otherwise, return the found node.
Execution Sample
DSA Typescript
function lowestCommonAncestor(root, p, q) {
  if (!root || root === p || root === q) return root;
  const left = lowestCommonAncestor(root.left, p, q);
  const right = lowestCommonAncestor(root.right, p, q);
  if (left && right) return root;
  return left || right;
}
Finds the lowest common ancestor of nodes p and q in a binary tree by recursive search.
Execution Table
StepOperationNode VisitedLeft Recursion ResultRight Recursion ResultLCA DecisionVisual State
1Start at root3??Recurse left and right3
2Recurse left5??Recurse left and right3 -> 5
3Recurse left of 56nullnullReturn 63 -> 5 -> 6
4Recurse right of 52nullnullReturn 23 -> 5 -> 6,2
5At node 5562Both sides non-null, return 5 as LCA3 -> 5(LCA)
6Recurse right of 31??Recurse left and right3(LCA) -> 1
7Recurse left of 10nullnullReturn 03(LCA) -> 1 -> 0
8Recurse right of 18nullnullReturn 83(LCA) -> 1 -> 0,8
9At node 1108Both sides non-null, return 13(LCA) -> 1
10At root 3351Both sides non-null, return 3 as LCA3(LCA)
11End351LCA found: 33(LCA)
💡 Recursion ends when all nodes visited and LCA found at node 3 where left and right recursion both found target nodes.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 9After Step 10Final
root355133
left?65055
right?22811
LCAnullnull5133
Key Moments - 3 Insights
Why do we return the node itself if root matches p or q?
Because if the current node is p or q, it could be the ancestor or one of the targets. Returning it helps identify if this path contains p or q, as shown in execution_table steps 1 and 3.
Why do we return root when both left and right recursion results are non-null?
Because it means p and q are found in different subtrees of the current node, so this node is their lowest common ancestor. See execution_table step 5 and 10.
What if only one side returns a non-null node?
We return that non-null node upwards because it means both p and q are in that subtree or that subtree contains one of them. This is shown in steps 9 and 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the LCA decision?
AReturn node 5 as LCA because both left and right are non-null
BReturn null because no target found
CReturn node 6 because it is left child
DReturn node 2 because it is right child
💡 Hint
Check the 'LCA Decision' column at step 5 in execution_table
At which step does the recursion find the final LCA node 3?
AStep 3
BStep 5
CStep 10
DStep 7
💡 Hint
Look at the 'LCA Decision' and 'Node Visited' columns in execution_table
If node q was not in the tree, how would the LCA change in the variable_tracker?
ALCA would be null at the end
BLCA would be node p
CLCA would be root node 3 anyway
DLCA would be node q
💡 Hint
Check how LCA is assigned when only one side returns a non-null node in variable_tracker
Concept Snapshot
Lowest Common Ancestor (LCA) in Binary Tree:
- Start at root node
- If root is null or matches p or q, return root
- Recursively find LCA in left and right subtrees
- If both sides return non-null, current node is LCA
- Else return non-null side
- Recursion ends when LCA found or subtree exhausted
Full Transcript
The Lowest Common Ancestor algorithm starts at the root node of the binary tree. If the current node is null or matches either of the target nodes p or q, it returns that node immediately. Otherwise, it recursively searches the left and right subtrees. If both recursive calls return non-null nodes, it means p and q are found in different subtrees, so the current node is their lowest common ancestor. If only one side returns a non-null node, that node is returned upwards as a candidate. This process continues until the recursion completes and the LCA is found. The execution table traces these recursive calls and decisions step-by-step, showing how the algorithm narrows down to the LCA node. The variable tracker records the state of key variables during execution. Key moments clarify why certain returns happen, helping beginners understand the recursion and decision points.