|
||
Title: Nth Largest node of BST Post by hary on Feb 20th, 2010, 8:34am Ofcourse an O(n) solution is clear to me. But i wish to know, can we do it in one pass. |
||
Title: Re: Nth Largest node of BST Post by towr on Feb 20th, 2010, 1:56pm Of course, just do a reverse inorder traversal and take the Nth value. |
||
Title: Re: Nth Largest node of BST Post by ravikumarv on Feb 24th, 2010, 12:22am Towr, How to do reverse in order traversal |
||
Title: Re: Nth Largest node of BST Post by towr on Feb 24th, 2010, 12:46am It's like a regular inorder traversal, except everywhere where you would choose the left child you now choose the right and vice versa. |
||
Title: Re: Nth Largest node of BST Post by srikanth.iiita on Feb 25th, 2010, 12:59pm @Towr .. why do we have to reverse inorder traversal.. tell me whether this approach is right or not... int count=0; inorder(root,n ) { inorder(root->left); count++; if(count==n) print(root->data); inorder(root->right); } |
||
Title: Re: Nth Largest node of BST Post by towr on Feb 25th, 2010, 1:39pm on 02/25/10 at 12:59:20, srikanth.iiita wrote:
Your code would print the Nth smallest. |
||
Title: Re: Nth Largest node of BST Post by srikanth.iiita on Feb 26th, 2010, 12:31am @towr thanks for the explanation.... |
||
Title: Re: Nth Largest node of BST Post by newCoder on Feb 27th, 2010, 11:17am I think this should work... Code:
|
||
Title: Re: Nth Largest node of BST Post by amitkm on Apr 8th, 2010, 11:11pm on 02/20/10 at 13:56:48, towr wrote:
I am not able to stop the traversal when after the nth max is found. The code below will do a full traversal. void FindNth(Node root,int nth, int counter) { if root == null return; FindNth(root.Right, n, counter); if(count == n) print root.Data FindNth(root.Left, n, counter); } |
||
Title: Re: Nth Largest node of BST Post by kaka189 on Apr 9th, 2010, 2:17am on 04/08/10 at 23:11:40, amitkm wrote:
hmm.. add a return value 1 if found and 0 if not found, call left only if right returns 0.Made changes to your code, ignored other major errors :) fix them int FindNth(Node root,int nth, int counter) { int ret =0; if root == null return; ret = FindNth(root.Right, n, counter); if(count == n) { print root.Data return 1; } if(!ret) ret = FindNth(root.Left, n, counter); return ret; } |
||
Powered by YaBB 1 Gold - SP 1.4! Forum software copyright © 2000-2004 Yet another Bulletin Board |