Блог пользователя Flawless

Автор Flawless, 11 лет назад, По-английски

What is the most efficient way to find Lowest Common Ancestor in Binary Tree ?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
11 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

See at TopCoder Algorithm Tutorials, there is a nice explanation for Lowest Common Ancestor. http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor

»
11 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

You can use sparse table or RMQ , i think.

  • »
    »
    11 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    i was trying to implement recursively.. but i think using RMQ would be best method

»
11 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

if you don't need online LCA algorithm, I think Tarjan's off-line LCA algorithm would be best one. easy to code an fast enough always :D (maybe fastest)

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

These are by far the best tutorials I've come across:
1. Finding the lowest common ancestor in rooted trees
2. What are the specifics in implementing an O(N log N) Lowest Common Ancestor algorithm?

Both use the Binary Raising method to calculate LCA. It's the best and most intuitive approach for LCA. Most of the top programmers in my country use this.