Created
July 30, 2014 06:07
-
-
Save KodeSeeker/4f255c1b3642e40db42f to your computer and use it in GitHub Desktop.
Nth largest element of a binary search tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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