0
0
DSA Javascriptprogramming~10 mins

Lowest Common Ancestor in Binary Tree in DSA Javascript - 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
No
Recurse left subtree
Recurse right subtree
If left and right both non-null
Return root as LCA
Return non-null child (left or right)
Start from root, check if it matches either target node or is null. Recurse left and right. If both sides return nodes, current root is LCA. Otherwise, return the non-null side.
Execution Sample
DSA Javascript
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 ResultRight ResultReturn ValueVisual State
1Start at root33
2Check root == p or q or null3No3
3Recurse left subtree53 -> 5
4Check root == p or q or null5No3 -> 5
5Recurse left subtree63 -> 5 -> 6
6Check root == p or q or null6Yes (node == p)3 -> 5 -> 6
7Return node 6663 -> 5 -> 6
8Recurse right subtree23 -> 5 -> 6, 2
9Check root == p or q or null2No3 -> 5 -> 6, 2
10Recurse left subtree73 -> 5 -> 6, 2 -> 7
11Check root == p or q or null7Yes (node == q)3 -> 5 -> 6, 2 -> 7
12Return node 7773 -> 5 -> 6, 2 -> 7
13Recurse right subtree43 -> 5 -> 6, 2 -> 7, 4
14Check root == p or q or null4No3 -> 5 -> 6, 2 -> 7, 4
15Recurse left subtreenull3 -> 5 -> 6, 2 -> 7, 4
16Return nullnullnull3 -> 5 -> 6, 2 -> 7, 4
17Recurse right subtreenull3 -> 5 -> 6, 2 -> 7, 4
18Return nullnullnull3 -> 5 -> 6, 2 -> 7, 4
19Left and right null, return null4nullnullnull3 -> 5 -> 6, 2 -> 7, 4
20Left result 7, right null, return 727null73 -> 5 -> 6, 2 -> 7
21Left result 6, right result 7, both non-null, return root 336733 -> 5 -> 6, 2 -> 7
💡 Returned root 3 as lowest common ancestor because both left and right subtrees contain p and q.
Variable Tracker
VariableStartAfter Step 6After Step 12After Step 20Final
root36723
leftundefined6666
rightundefinedundefined777
Return Valueundefined6773
Key Moments - 3 Insights
Why do we return root immediately if root matches p or q?
Because if root is p or q, it could be the ancestor or one of the nodes itself. See step 6 and 11 in execution_table where root matches p or q and returns immediately.
Why do we return root when both left and right recursive calls are non-null?
Because it means p and q are found in different subtrees, so current root is their lowest common ancestor. See step 21 where both left (6) and right (7) are non-null, so root 3 is returned.
What happens if one subtree returns null?
We return the non-null subtree result because both nodes are in that subtree or one node is ancestor of the other. See step 20 where left is 7 and right is null, so 7 is returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the return value and why?
AReturn null because node 6 is not p or q
BReturn node 6 because it matches p
CReturn root 3 because it is the ancestor
DReturn node 5 because it is the parent
💡 Hint
Check the 'Return Value' column at step 6 and the 'Operation' column for matching p or q.
At which step does the function decide that node 3 is the lowest common ancestor?
AStep 12
BStep 20
CStep 21
DStep 6
💡 Hint
Look for the step where both left and right results are non-null and root is returned.
If node q was not in the tree, how would the return value at step 21 change?
AReturn null because q is missing
BReturn node 6 because only p is found
CReturn node 7 because q is missing
DReturn root 3 anyway
💡 Hint
Refer to the logic in step 20 where one subtree returns a node and the other returns null.
Concept Snapshot
Lowest Common Ancestor (LCA) in Binary Tree:
- 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, root is LCA.
- Else return the non-null side.
- Works by bottom-up recursion checking presence of p and q.
Full Transcript
The Lowest Common Ancestor algorithm starts at the root node. If the root is null or matches either target node p or q, it returns root immediately. Otherwise, it recursively searches the left and right subtrees for p and q. If both left and right recursive calls return non-null nodes, it means p and q are found in different subtrees, so the current root is their lowest common ancestor. If only one side returns a non-null node, that node is returned upwards. The process continues until the LCA is found or the search ends. This is shown step-by-step in the execution table where nodes 6 and 7 are found in left and right subtrees of root 3, making 3 the LCA.