Pergunta de entrevista da empresa LinkedIn

Given an unordered binary tree and any two distinct nodes in the tree, find their common ancestor.

Resposta da entrevista

Sigiloso

20 de ago. de 2017

This was pretty straightforward. I wrote an algorithm that did an exhaustive search by traversing the whole tree for some target element; if the element is found, it returns the path it took from the root to the element. To get the answer, run this search on the two target elements and compare their paths. The two nodes' common ancestor is the last node in the search path that are equal.