Lowest Common Ancestor Algorithm

前端 未结 6 1876
感动是毒
感动是毒 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:28

    I have one simplistic solution sort the two elements and the lowest be left and highest be right visit root def recurse(root) return nil if root.empty? if left <= root && right >= root return root elsif left <= root && right <= root recurse(root.left) else recurse(root.right) end

    So this will check on each traversal The problem soluted in O(log n) time for average and worst and O(log

提交回复
热议问题