Created
September 6, 2021 23:32
-
-
Save misterpoloy/a24b9f60b46652fe977f977095cdcf17 to your computer and use it in GitHub Desktop.
Find k largest value in a binary search tree using javascript
This file contains 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
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 | |
} |
Author
misterpoloy
commented
Sep 6, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment