Pergunta de entrevista da empresa YouTube

Find the least common ancestor of 2 nodes in a binary tree...

Respostas da entrevista

Sigiloso

29 de jul. de 2010

The answer is not correct. The question says binary tree only. BT != BST

Sigiloso

27 de mai. de 2010

The main idea of the solution is — While traversing Binary Search Tree from top to bottom, the first node n we encounter with value between n1 and n2, i.e., n1 < n < n2 is the Lowest or Least Common Ancestor(LCA) of n1 and n2 (where n1 < n2). So just traverse the BST in pre-order, if you find a node with value in between n1 and n2 then n is the LCA, if it's value is greater than both n1 and n2 then our LCA lies on left side of the node, if it's value is smaller than both n1 and n2 then LCA lies on right side.

1