Lowest Common Ancestor in Binary Search Tree

Lowest Common Ancestor of a Binary Search Tree Problem

Given a binary search tree (BST) with unique node values and two nodes p and q, return their lowest common ancestor (LCA).

The LCA is the lowest node in the tree such that both p and q are its descendants. A node can also be considered as its own descendant.

Lowest Common Ancestor in Binary Search Tree Problem

Example 1:

Lowest Common Ancestor in Binary Search Tree Problem 1

Example 2:

Lowest Common Ancestor in Binary Search Tree Problem 2

Constraints:

  • 2 <= Number of nodes in the tree <= 100.
  • -100 <= Node.val <= 100
  • p != q
  • p and q will both exist in the BST.

Hints to Solve Lowest Common Ancestor of a Binary Search Tree

Recommendation for Time and Space Complexity:

Aim for a solution with a time complexity of O(h) and a space complexity of O(h) or better, where h is the height of the tree.

Hint 1 :

  • Binary Search Tree (BST) follows the property where all nodes in the left subtree are smaller than the current node, and all nodes in the right subtree are greater.
  • This property holds for every node in the tree.
  • The Question is – Can you leverage this rule to identify the Lowest Common Ancestor (LCA) of the given nodes?

Hint 2 :

  • Use recursion to traverse the tree. Think about the conditions where you decide to move left or right in the tree based on the values of the two nodes.
  • How can these conditions help pinpoint the LCA?

Hint 3 :

  • When the two nodes lie in different subtrees, the current node becomes the LCA because a split occurs.
  • If both nodes are in the left or right subtree, the LCA must be there, so you continue traversing in that direction.

Hint 4 :

  • If the current node matches one of the given nodes p or q, it is the LCA.
  • This is because encountering p or q during the traversal automatically makes that node the lowest ancestor.

There are mainly 2 approach to solve this problem:

  1. Recursion Method
  2. Iteration Method

1. Recursion Method

Use the BST property to traverse the tree recursively. At each step, decide whether to move left, right, or return the current node as the LCA based on the values of p and q.

  • Time complexity: O(h)
  • Space complexity: O(h)

where h is height of the tree.

Code

2. Iteration Method

Instead of recursion, use a loop to traverse the tree. At each node, check the values of p and q to determine the direction (left, right, or current node) until the LCA is found.

  • Time complexity: O(h)
  • Space complexity: O(1)

Code

More Articles