Finding the common ancestor in a binary tree

后端 未结 10 1068
悲哀的现实
悲哀的现实 2021-02-09 02:50

This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a point

10条回答
  •  滥情空心
    2021-02-09 03:35

    Maybe silly approach:

    Generate the path from each node to the root, storing it as a string of "L" and "R".

    Reverse these strings. Take the longest common prefix - you now have the path to the common ancestor.

提交回复
热议问题