Hi friends,
   I need a logic to find Least Common ancestor for 2 given nodes for Binary as 
well as Binary search tress.  Actually I tried in google.com also , but I am 
unable to understand . 
  I solved upto some extent like ..
  First I need to number nodes in Inorder traversal way . So all leaves will be 
in odd number like so.
  if there are two nodes with numbers (Inorder numbers ) as 21 and 16 then
  the binary rep of 21 is 10101 and for 16 is 10000 . so on Xoring both I got 
as 01010 . Means the common path for both nodes is two second level . so the 
ancestor should be at the height of second level . so I have added common 1s 
which are at left end . so now the number is 11010 . 
    I strucked up here . If my algorith is wrong , please correct me .
   
  Thanks Regards,
  Gopi.k


      
  GopiKrishna Komanduri
Software engineer
Hyderabad
[EMAIL PROTECTED]

   




       
---------------------------------
 5, 50, 500, 5000 - Store N number of mails in your inbox. Click here.

[Non-text portions of this message have been removed]

Reply via email to