Find k | A BST node ; Recursive function to insert an key into BST ; Function to find k 'th largest element in BST  Here count denotes the number  of nodes processed so far ; base case ; search in left subtree ; if k 'th smallest is found in left subtree, return it ; if current element is k 'th smallest, return it ; else search in right subtree ; Function to find k 'th largest element in BST ; maintain an index to count number of nodes processed so far ; Driver code
class Node { constructor ( x ) { this . data = x ; this . left = null ; this . right = null ; } } let count = 0 ; function insert ( root , x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) root . left = insert ( root . left , x ) ; else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } function kthSmallest ( root , k ) { if ( root == null ) return null ; let left = kthSmallest ( root . left , k ) ; if ( left != null ) return left ; count ++ ; if ( count == k ) return root ; return kthSmallest ( root . right , k ) ; } function printKthSmallest ( root , k ) { count = 0 ; let res = kthSmallest ( root , k ) ; if ( res == null ) document . write ( " " + " " ) ; else document . write ( " " + " " + res . data ) ; } let root = null ; let key = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] ; for ( let i = 0 ; i < key . length ; i ++ ) { root = insert ( root , key [ i ] ) ; } let k = 3 ; printKthSmallest ( root , k ) ;