更新时间:2023-01-17 19:45:49
恒定空间回答:(虽然不一定有效)。
Constant space answer: (although not necessarily efficient).
有一个函数findItemInPath(int index,int searchId,Node root)
Have a function findItemInPath(int index, int searchId, Node root)
然后从0深度的树迭代,在两个搜索路径中找到第0个项目,第1个项目等。
then iterate from 0 .. depth of tree, finding the 0-th item, 1-th item etc. in both search paths.
当你找到我时这样函数返回两者的结果相同,但不是i + 1,
,那么路径中的第i项是最低共同祖先。
When you find i such that the function returns the same result for both, but not for i+1, then the i-th item in the path is the lowest common ancestor.