Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
Created July 30, 2014 06:07
Show Gist options
  • Select an option

  • Save KodeSeeker/4f255c1b3642e40db42f to your computer and use it in GitHub Desktop.

Select an option

Save KodeSeeker/4f255c1b3642e40db42f to your computer and use it in GitHub Desktop.
Nth largest element of a binary search tree
void nthLargest( Node root, int N)
{
/*
Idea : Do reverse in order traversal and keep checking for value of N
against counter. If counter==N. Then print value.
*/
private static int counter=0;
if (root==null)
return;
//go Right to get to the largest element.
nthLargest(root.getRight(),N);
// now in order check
if(++counter==N)
{
//print it!
System.out.print(root.data);
}
// go Left.
nthLargest(root.getLeft(),N);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment