Skip to content

Instantly share code, notes, and snippets.

@FermiDirak
Created March 12, 2018 16:49
Show Gist options
  • Save FermiDirak/a3e8d7bf1ca175ec49c5fdb75326c214 to your computer and use it in GitHub Desktop.
Save FermiDirak/a3e8d7bf1ca175ec49c5fdb75326c214 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function(root, k) {
k = k - 1; // zero-index k
var smallestValue;
var path = [];
var goDown = function(root) {
while (root.left !== null) {
root = root.left;
path.push('left');
}
}
var getNextSmallest = function(root, k, currentSmallestIndex) {
if (k === currentSmallest) {
return;
}
var currentNode = getCurrentNode(root, path);
}
var getCurrentNode = function(root, path) {
for (var i = path.length - 1; i >= 0; --i) {
var operation = path[i];
if (operation === 'left') {
root = root.left;
} else {
root = root.right;
}
}
return root;
}
goDown(root);
getNextSmallest(root, k, 0);
return smallestValue;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment