Reported April 2025
Motivebinary tree

Lowest Common Ancestor in a Binary Tree

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

With 100000 nodes possible, the Motive OA reported in April 2025 won't let you brute-force every pair of root-to-node paths and compare them. Well, you could, but it's wasteful and risky. This is Lowest Common Ancestor on a plain binary tree, not a BST, so you can't steer left or right by comparing values. Both targets are guaranteed to exist and a node counts as its own descendant. That's a tree recursion problem with one clean pass. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net, but the pattern below is short enough to own tonight.

The problem

Given a binary tree and two distinct nodes in it, return the value of their lowest common ancestor. A node counts as a descendant of itself.
For this exercise, the runner supplies the target nodes by their unique integer values p and q, and returns the ancestor value instead of a node reference. Both targets exist in the tree. The tree is an ordinary binary tree, not necessarily a binary search tree.

Function
lowestCommonAncestorValue(root: TreeNode, p: int, q: int) → int

Examples
Example 1
root = [3,5,1,6,2,0,8,null,null,7,4]
p = 5
q = 1
return = 3
The first common ancestor is the root.
Example 2
root = [3,5,1,6,2,0,8,null,null,7,4]
p = 5
q = 4
return = 5
A node can be its own descendant, so 5 is the common ancestor.
Example 3
root = [1,2]
p = 1
q = 2
return = 1
The root is one of the requested nodes.

Constraints
The tree contains 2 through 100000 nodes.
-1000000000 <= node.val <= 1000000000; all node values are unique.
p != q and both target values occur in the tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a single post-order DFS. At each node, if it's null or its value equals p or q, return it. Otherwise recurse left and right. If both sides return something non-null, the current node is the split point, so it's the LCA. If only one side returns something, pass that up. That handles Example 2 naturally: hitting 5 returns immediately, and since the target 4 sits below it, 5 bubbles up as the answer. The early return on a match is what makes the self-descendant rule work for free. The common pitfall is recursion depth. With 100000 nodes, a skewed tree can blow the stack in some languages, so mention an iterative version using parent pointers if asked. Another pitfall is using BST logic on values. Don't. StealthCoder is the hedge if you freeze on the base case during the live OA, but this is about ten lines.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Lowest Common Ancestor in a Binary Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as lowest common ancestor of a binary tree. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lowest Common Ancestor in a Binary Tree FAQ

What's the trick to Lowest Common Ancestor in the Motive OA?+

Post-order recursion. Return the node if it's null or matches p or q. If both left and right subtrees return non-null, the current node is the LCA. Otherwise return whichever side is non-null. The early return on a match covers the case where one target is an ancestor of the other.

Can I use BST properties to speed this up?+

No. The problem says it's an ordinary binary tree, not a BST. Comparing p and q against the node value tells you nothing about which subtree holds them. You have to search both sides, which is still O(n) total time with one traversal.

What's the time and space complexity?+

Time is O(n) since each node is visited once. Space is O(h) for the recursion stack, where h is tree height. On a skewed tree with 100000 nodes, that's O(n), so know the iterative alternative using a parent map in case the interviewer asks.

How do I handle the case where one node is the ancestor of the other?+

You don't need special code. When the recursion hits p or q it returns immediately without exploring below. The other target sits beneath it, so the parent only sees one non-null side and passes that node up. Example 2 with p=5 and q=4 returns 5 this way.

How do I prepare for this in 48 hours?+

Write the recursive solution from memory three times, then trace Examples 1 and 2 by hand. After that, code the iterative version with a parent map and a set of ancestors. That covers both the standard answer and the stack-depth follow-up. Skip anything fancier like Tarjan's offline LCA.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it