wolfSyntax's blog

By wolfSyntax, 10 years ago, In English

where root = 1, Left nodes (L) = 2n and Right nodes (R) = 2n+1. In getting the Common Ancestor, we need to compare the ancestor of Left nodes and Right Nodes until the result is the same or reaches to 1 as their common ancestor.

(1)
               /            \
              /              \
            (2)               (3)
         /       \         /       \
        /         \       /         \
      (4)         (5)   (6)         (7)

For example, The ancestor of : > 2 and 3 is 1. > 5 and 6 is 1. > 4 and 5 is 2. > 6 and 7 is 3. > and so on.

While(true): if(L > R) Then: L = L << 1; else if(R > L) Then: R = R << 1; else: break;

print(R)

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it