Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created September 6, 2021 23:32
Show Gist options
  • Save misterpoloy/a24b9f60b46652fe977f977095cdcf17 to your computer and use it in GitHub Desktop.
Save misterpoloy/a24b9f60b46652fe977f977095cdcf17 to your computer and use it in GitHub Desktop.
Find k largest value in a binary search tree using javascript
function postOrderTraverse(tree, target, nodeInfo) {
if (tree === null || nodeInfo.visited >= target) return
postOrderTraverse(tree.right, target, nodeInfo)
if (nodeInfo.visited < target) {
nodeInfo.visited = nodeInfo.visited + 1
nodeInfo.lastValue = tree.value
postOrderTraverse(tree.left, target, nodeInfo)
}
}
function findKthLargestValueInBst(tree, k) {
// Write your code here.
const nodeInfo = { visited: 0, lastValue: -1 }
postOrderTraverse(tree, k, nodeInfo);
return nodeInfo.lastValue
}
@misterpoloy
Copy link
Author

screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment