> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/find-kth-largest-value-in-bst.md).

# Find Kth Largest Value In Bst

![](/files/Bfk4QpceYwQokXPZH0sN)

* 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;
```
