Re: [algogeeks] least common ancestore bst

2013-06-05 Thread Ankit Sambyal
struct node { int data; struct node* left; struct node* right; }; struct node* newNode(int ); /* Function to find least comman ancestor of n1 and n2 */ int leastCommanAncestor(struct node* root, int n1, int n2) { /* If we have reached a leaf node then LCA doesn't exist If

Re: [algogeeks] least common ancestore bst

2013-05-27 Thread rahul sharma
then solution i posted aboive is correct in that case???plz comment On Fri, May 17, 2013 at 9:49 AM, avinesh saini avinesh.sa...@gmail.comwrote: If one node is parent of other then Parent Node is lowest common ancestor. Source- http://en.wikipedia.org/wiki/Lowest_common_ancestor (just read

Re: [algogeeks] least common ancestore bst

2013-05-27 Thread Mangal Dev Gupta
static Node LCA(Node root, int a, int b) { if (root == null) return null; if (root.getData() == a || root.getData() == b) return root; Node root1 = LCA(root.getLeft(), a, b); Node root2 = LCA(root.getRight(), a, b); if (root1 !=

Re: [algogeeks] least common ancestore bst

2013-05-16 Thread rahul sharma
[image: BST_LCA] what should be ancestor of 12 and 14.it should be 12 or 14...if 12 then for 20 and 22 also it is 20...if 12 ans 14 has ancestor as 8 then for 20 and 22 it is NULL. @allplz give me clear one line definition in case if one node is parent of other then

Re: [algogeeks] least common ancestore bst

2013-05-16 Thread avinesh saini
If one node is parent of other then Parent Node is lowest common ancestor. Source- http://en.wikipedia.org/wiki/Lowest_common_ancestor (just read it) On Mon, May 13, 2013 at 1:42 AM, rahul sharma rahul23111...@gmail.comwrote: [image: BST_LCA] what should be ancestor of 12 and 14.it

Re: [algogeeks] least common ancestore bst

2013-04-26 Thread Tushar Patil
@rahul : It's fine solution, but can we check the root-data == n1 || n2 before calling function recursively, I think if we check this condition 1st it will reduce unnecessary function calls. Correct me if i am wrong? Thanks, Tushar Patil. On Sun, Apr 21, 2013 at 10:26 PM, rahul sharma