3 ms·
The binary tree thing is simpler than it sounds; don't be faked out by the fact that it involves binary trees. You can traverse a binary tree from the root to a
by pjscott 14y ago
The binary tree thing is simpler than it sounds; don't be faked out by the fact that it involves binary trees. You can traverse a binary tree from the root to any node, and record the nodes on that path. Do so for each of the two nodes, then compare the two sequences, looking for the first node that is common to both of them. You can do this in O(lg n) if the binary tree is balanced, or O(n) if it is not. And if this sounds complicated in words, just draw a picture and it'll suddenly look simpler.