LeetCode Problem

How to Solve Lowest Common Ancestor of a Binary Search Tree

The problem requires finding the lowest common ancestor (LCA) of two nodes in a binary search tree. A key concept involves binary-tree traversal and state tracking to determine the lowest common ancestor efficiently.

GhostInterview Help

Need help with Lowest Common Ancestor of a Binary Search Tree without spending extra time grinding it?

GhostInterview can read Lowest Common Ancestor of a Binary Search Tree from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #235Binary-tree traversal and state trackingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem requires finding the lowest common ancestor (LCA) of two nodes in a binary search tree. A key concept involves binary-tree traversal and state tracking to determine the lowest common ancestor efficiently.

Problem Statement

Given a binary search tree (BST) and two nodes, p and q, you need to find the lowest common ancestor (LCA) of the two nodes. The LCA is defined as the lowest node in the tree that has both nodes as descendants. It is guaranteed that both nodes p and q will exist in the BST and that p is not equal to q.

To solve this problem, traverse the BST from the root. At each node, check the relative positions of p and q. If both p and q are in the left or right subtree of the current node, move in the respective direction. If p and q are in different subtrees, the current node is the LCA.

Examples

Example 1

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8

Output: 6

The LCA of nodes 2 and 8 is 6.

Example 2

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4

Output: 2

The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Example 3

Input: root = [2,1], p = 2, q = 1

Output: 2

Example details omitted.

Constraints

  • The number of nodes in the tree is in the range [2, 105].
  • -109 <= Node.val <= 109
  • All Node.val are unique.
  • p != q
  • p and q will exist in the BST.

Solution Approach

Binary Search Tree Property

Since the tree is a binary search tree, it has a property where for any node, values in the left subtree are smaller, and values in the right subtree are larger. This property helps in efficiently determining the direction of traversal (left or right) while searching for the LCA.

Depth-First Search (DFS)

Using a DFS traversal, we explore the tree starting from the root. At each node, check if both p and q lie in the left or right subtrees. If so, move to the respective subtree. If p and q lie in different subtrees, the current node is the LCA.

Track the state of traversal to avoid unnecessary rechecking of nodes. By comparing the values of the current node with p and q, we can decide whether to move left or right or return the current node as the LCA.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the solution depends on the depth of the tree, which can be O(log n) for a balanced tree and O(n) for a skewed tree. The space complexity is O(1) for the iterative approach or O(h) for the recursive DFS approach, where h is the height of the tree.

What Interviewers Usually Probe

  • Candidate correctly identifies the LCA concept and binary search tree traversal patterns.
  • Candidate efficiently explains the use of DFS and state tracking in LCA search.
  • Candidate discusses time and space complexities relevant to the tree's structure and traversal.

Common Pitfalls or Variants

Common pitfalls

  • Confusing binary tree traversal with BFS, leading to inefficient solutions.
  • Incorrectly identifying the LCA when both nodes are in the same subtree.
  • Not considering the edge case where one of the nodes is the root.

Follow-up variants

  • Find the LCA for multiple pairs of nodes in a single BST.
  • Find the LCA in a binary tree (not necessarily a BST).
  • Find the LCA in a tree with non-unique node values.

How GhostInterview Helps

  • GhostInterview helps break down the problem into clear steps, focusing on binary search tree properties.
  • The solution approach emphasizes key traversal patterns and state management for identifying the LCA efficiently.
  • GhostInterview identifies potential pitfalls and areas where the candidate might overcomplicate or misinterpret the problem.

Topic Pages

FAQ

How do I approach the Lowest Common Ancestor problem in a Binary Search Tree?

Start by utilizing the BST property to narrow down the search direction. Use DFS to explore the tree and track the state of traversal to identify the LCA.

What is the key traversal method for solving the Lowest Common Ancestor problem?

The primary traversal method is Depth-First Search (DFS), where you explore each subtree and determine if both nodes are present in the same subtree.

What should I consider when analyzing the time complexity for this problem?

The time complexity depends on the tree's height, which is O(log n) for a balanced tree and O(n) for a skewed tree. Space complexity is O(h) for a recursive approach.

What is the significance of the binary search tree property in this problem?

The binary search tree property allows for efficient traversal and narrowing down the search for the LCA by checking whether the nodes lie in the left or right subtree of the current node.

How can GhostInterview help me with the Lowest Common Ancestor problem?

GhostInterview provides a structured approach, highlighting the critical traversal patterns and common pitfalls to avoid, ensuring an efficient and clear solution.

GhostInterview Solver

Need direct help with Lowest Common Ancestor of a Binary Search Tree instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Lowest Common Ancestor of a Binary Search Tree from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.