Skip to content
RED SAGSkills
hard20 pts · first answer countsgraphsbinary-treeslca

LCA in a tree without BST order

In this LCA function for a general binary tree, both p and q are known to exist. What does it return when p is an ancestor of q?

function lca(n, p, q) {
  if (!n || n === p || n === q) return n
  const l = lca(n.left, p, q)
  const r = lca(n.right, p, q)
  if (l && r) return n
  return l || r
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions