Faaez Razeen

Lowest Common Ancestor of a Binary Search Tree

  • 1 min read
  • DFS
  • BFS
  • Tree
  • Blind75
  • LC-Medium
  • Binary Tree
  • Binary Search Tree

3 years ago

Approach: Check where split occurs

def lowestCommonAncestor(root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': cur = root while cur: if cur.val < p.val and cur.val < q.val: cur = cur.right elif cur.val > p.val and cur.val > q.val: cur = cur.left else: return cur