[algogeeks] Re: least common ancestore bst

2013-04-22 Thread Don
There is no need to use recursion here. Tail recursion is essentially using recursion as an expensive loop. int leastCommonAncestor(struct node *root, int n1, int n2) { while(root) { if ((root->data == n1) || (root->data == n2) || ((root->data > n1) != (root->data > n2))) retu

Re: [algogeeks] Re: least common ancestore bst

2013-04-21 Thread rahul sharma
I am not clear about the definition.Plz confirm in case one node is parent of other then whether that parent node is ancestor or it is -1.And plz confirm above code if it miss any case On Mon, Apr 22, 2013 at 1:13 AM, Dave wrote: > @Rahul: You must know if a node is its own ancestor or not. Thi

[algogeeks] Re: least common ancestore bst

2013-04-21 Thread Dave
@Rahul: You must know if a node is its own ancestor or not. This is a matter of definition. If a node is its own ancestor, and if n1 is an ancestor of n2, then n1 is the least common ancestor of n1 and n2. If a node is not its own ancestor, and if n1 is an ancestor of n2, then the parent o