Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread atul verma
Apologizes .. I miss read the question. On Thu, Apr 8, 2010 at 12:14 PM, vivek bijlwan wrote: > @atul it is not a BST > > > On Thu, Apr 8, 2010 at 10:19 AM, atul verma wrote: > >> Its very simple to solve this. >> >> Start from root. >> >> Compare the value of current node data value to both n

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread Ashish Mishra
yup atul algo is correct (cur_data - node->right) * ( cur_data- node->left) < -1 for ancestors On Thu, Apr 8, 2010 at 10:19 AM, atul verma wrote: > Its very simple to solve this. > > Start from root. > > Compare the value of current node data value to both nodes. > > 1. if both are greater tha

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread Anurag Sharma
Dear Himanshu, Here is what I think. This problem can be solved easily by using recursion. Here is the pseudo code for it: Logic: Let 'A' and 'B' be the given too node whose common ancestor we have to find. Now perform an inorder traversal of the tree and at every node call the 'search(A)' functio

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread Anurag Sharma
Well Atul, Mind it, its not a Binary Search Tree, its just a Binary Tree. So this concept of the elements in left sub tree all having the value less than the current node and similar for the right subtree will not stand here. Anurag Sharma http://anuragsharma-sun.blogspot.com/ On Thu, Apr 8, 201

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread vivek bijlwan
@atul it is not a BST On Thu, Apr 8, 2010 at 10:19 AM, atul verma wrote: > Its very simple to solve this. > > Start from root. > > Compare the value of current node data value to both nodes. > > 1. if both are greater than current node then traverse node->right > 2. if both are lesser than curr

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-08 Thread Ashish Mishra
i think u mean lowest commen ancestor? On Wed, Apr 7, 2010 at 10:34 PM, Himanshu Aggarwal wrote: > For a given binary tree, given the root and two node pointers, how can we > find their youngest common ancestor. > > Say the node is like: > > struct node{ >int data; >struct node*l

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-07 Thread atul verma
Its very simple to solve this. Start from root. Compare the value of current node data value to both nodes. 1. if both are greater than current node then traverse node->right 2. if both are lesser than current node then traverse node->left 3. else return current node pointer. Any comments, Atu

Re: [algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-07 Thread Pramod Negi
could you please elucidate more?? On Wed, Apr 7, 2010 at 10:34 PM, Himanshu Aggarwal wrote: > For a given binary tree, given the root and two node pointers, how can we > find their youngest common ancestor. > > Say the node is like: > > struct node{ >int data; >struct node*left, *

[algogeeks] Finding youngest common ancestor of two nodes in a binary tree

2010-04-07 Thread Himanshu Aggarwal
For a given binary tree, given the root and two node pointers, how can we find their youngest common ancestor. Say the node is like: struct node{ int data; struct node*left, *right; }; i.e the father field is not there. Please note that it is not a binary search tree, but just a b