If you maintain the parent, it'll be a simple problem. Just go to the two nodes and then trace their parents till you reach the common node.
On Tue, Nov 22, 2011 at 1:59 PM, anshu mishra <anshumishra6...@gmail.com>wrote: > first try to understand the sol then comment. it is for binary tree not > for BST. > > On Mon, Nov 21, 2011 at 10:25 PM, Piyush Grover <piyush4u.iit...@gmail.com > > wrote: > >> For BST it would be rather simpler. find the first node which lies in >> between the two. >> >> On Wed, Nov 16, 2011 at 1:44 PM, anshu mishra >> <anshumishra6...@gmail.com>wrote: >> >>> Node *LCA(node *root, node *e1, node *e2, int &x) >>> >>> { >>> >>> Node *temp =NULL; >>> >>> Int y = 0; >>> >>> If (root->left) temp = LCA(root->left, e1, e2, y); >>> >>> x+=y; >>> >>> if (temp) return temp; >>> >>> if (x==2) return node; >>> >>> y = 0; >>> >>> If (root->right) temp = LCA(root->right, e1, e2, y); >>> >>> x+=y; >>> >>> if (temp) return temp; >>> >>> if (x==2) return node; >>> >>> If (root == e1 || root == e2) x++; >>> >>> Return null; >>> >>> } >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Anshuman Mishra | Software Development Engineer | Amazon > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Regards *Abhishek* -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.