# Find Kth Largest Value In Bst

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-374bd196c1b4574430e30c84d4a6c3000dc073f2%2FScreenshot%202023-01-22%20at%2019.14.30.png?alt=media)

* h + k, h

```jsx
// This is an input class. Do not edit.
class BST {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class Memo {
  constructor() {
    this.count = 0;
    this.value;
  }
}

function findKthLargestValueInBst(tree, k) {
  const memo = new Memo();
  findValue(tree, k, memo);
  return memo.value;
}

function findValue(node, k, memo) {
  if (node === null || memo.value !== undefined) {
    return;
  }

  const right = findValue(node.right, k, memo);
  memo.count += 1;
  if (memo.count === k)
    memo.value = node.value;
  
  const left = findValue(node.left, k, memo);
  
  return;
}

// Do not edit the lines below.
exports.BST = BST;
exports.findKthLargestValueInBst = findKthLargestValueInBst;
```
