Problem
Ask
Submissions

Problem: Lowest Common Ancestor of a Binary Search Tree

Medium
30 min
Try to solve the Lowest Common Ancestor of a Binary Search Tree problem.

Statement

Given a binary search tree with nn nodes, your task is to find the lowest common ancestor of two of its nodes, node1 and node2.

The lowest common ancestor of two nodes, node1 and node2, is defined as the lowest node in the binary search tree that has both node1 and node2 as descendants.

By definition, a node is a descendant of itself. For example, if node2 is a descendant of node1, and we know that node1 is a descendant of itself, then node1 will be the lowest common ancestor of node1 and node2.

Constraints:

  • 2n5002 \leq n \leq 500
  • 103-10^3 \leq Node.data 103\leq 10^3
  • All Node.data are unique.
  • node1 \neq node2
  • node1 and node2 exist in the tree.
Problem
Ask
Submissions

Problem: Lowest Common Ancestor of a Binary Search Tree

Medium
30 min
Try to solve the Lowest Common Ancestor of a Binary Search Tree problem.

Statement

Given a binary search tree with nn nodes, your task is to find the lowest common ancestor of two of its nodes, node1 and node2.

The lowest common ancestor of two nodes, node1 and node2, is defined as the lowest node in the binary search tree that has both node1 and node2 as descendants.

By definition, a node is a descendant of itself. For example, if node2 is a descendant of node1, and we know that node1 is a descendant of itself, then node1 will be the lowest common ancestor of node1 and node2.

Constraints:

  • 2n5002 \leq n \leq 500
  • 103-10^3 \leq Node.data 103\leq 10^3
  • All Node.data are unique.
  • node1 \neq node2
  • node1 and node2 exist in the tree.