First Common Ancestor of a Binary Tree

折月煮酒 提交于 2019-12-12 07:19:46

问题


If I have a binary search tree like this then what will be lowest common ancestor of nodes 6 and 1?


回答1:


According to the Wikipedia definition of the Lowest common ancestor I correct myself:

The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with n nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).

So yes going by this definition the correct answer would be 6. If this is an interview question would be good to clarify in advance with the interviewer.



来源:https://stackoverflow.com/questions/10133332/first-common-ancestor-of-a-binary-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!