Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Lowest Common Ancestor of a Binary Search Tree (medium)

Problem Statement

Given a binary search tree (BST) and two of its nodes, find the node that is the lowest common ancestor (LCA) of the two given nodes. The LCA of two nodes is the node that lies in between the two nodes in terms of value and is the furthest from the root. In other words, it's the deepest node where the two nodes diverge in the tree. Remember, in a BST, nodes have unique values.

Examples

  • Input:
    • BST: [6,2,8,0,4,7,9,null,null,3,5]
    • Node 1: 2
    • Node 2: 8
  • Expected Output: 6
  • Justification: The nodes 2 and 8 are on the left and right children of no

.....

.....

.....

Like the course? Get enrolled and start learning!