Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach) @BackToBackSWE
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)  @BackToBackSWE
Uploaded April 2019 | Updated November 2026, 11 hours ago
Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
πŸ“Ή Intuitive Video Explanations
πŸƒ Run Code As You Learn
πŸ’Ύ Save Progress
❓New Unseen Questions
πŸ”Ž Get All Solutions

Question: Given the root of a binary tree and 2 references of nodes that are in the binary tree, find the lowest common ancestor of the 2 nodes. The nodes do not have parent pointers.

The Approach

So there are many modulations of this problem where you can build a hashtable and make parent pointers, etc. We will focus on the recursive solution.


The Algorithm

The key is that we want to root ourselves at a node and then search left and then right for either of the 2 nodes given.

If we see either node, we will return it, if we do not find the node in a subtree search the value of null will be returned and bubbled up.

After we search both left and right we ask ourselves what our results mean.

If we found nothing to the left, we just bubble up what is on the right (whatever that search result may be). This node we sit at cannot be the LCA since the left and right did not yield the 2 nodes we want.

If we found nothing to the right, we just bubble up what is on the left (whatever that search result may be). This node we sit at cannot be the LCA since the left and right did not yield the 2 nodes we want.

If both the right and left result are not null, we have found our LCA.


Why?

We know it is an ancestor at the least but we definitely know it is the lowest common ancestor because we went bottom upwards, whatever we hit will be the LCA and it will bubble up.


Complexities

Time: O( n )

We will be touching the whole tree in the search, there are n nodes in the tree and we do O(1) work at each node. There are not exactly n calls though but I need to double check this...I need to solve the recurrence but oh well...we know it will stay linear in the asymptotic regard.

Space: O( h )

Stack usage at maximum will be the trees height. Worst case would be O(n) if our tree is skewed purely to the left or right and we need to find deep nodes. But n IS h in that case. But we say O( n ) in that case since it is more accurate to what is happening, the tree's size in nodes dominating the height.

++++++++++++++++++++++++++++++++++++++++++++++++++

HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww

Tuschar Roy: youtube.com/user/tusharroy2525

GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ

Jarvis Johnson: youtube.com/user/VSympathyV

Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)Reverse Polish Notation: Types of Mathematical Notations & Using A Stack To Solve RPN Expressions4 Tips To Learn Java Programming As Fast As Possible As A BeginnerProfessor Clyde Kruskal On Kruskals AlgorithmPartition To K Equal Sum Subsets From An Array of Integers - The Backtracking ApproachAdd Two Numbers Without The + Sign (Bit Shifting Basics)Compute The Next Permutation of A Numeric Sequence - Case Analysis (Next Permutation on Leetcode)Serialize & Deserialize A Binary Tree - Crafting Recursive Solutions To Interview ProblemsGenerate All Strings With n Matched Parentheses - Backtracking (Generate Parentheses on LeetCode)The Quicksort Sorting Algorithm: Pick A Pivot, Partition, & RecurseA Detailed Algorithmic Analysis of Insertion Sort. Best Case & Worst Case.Increment An Integer Represented As An Array (Plus One on LeetCode)
Back To Back SWE |

Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER