Lowest Common Ancestor Algorithm

前端 未结 6 1724
感动是毒
感动是毒 2021-02-05 17:42

So I have been looking into implementing a lowest common ancestor algorithm. I looked at many different algorithms (mainly variations of Trajan\'s solution or variations of the

6条回答
  •  梦谈多话
    2021-02-05 18:07

    Your algorithm is quadratic, but it can easily be made linear.

    Just use hashtable (i.e. set) for parentNode, instead of list. Thus checking whether a node is in parentNode will be O(1) instead of O(n).

提交回复
热议问题