Skip to content

Instantly share code, notes, and snippets.

@kennyxcao
Created October 16, 2017 16:59
Show Gist options
  • Save kennyxcao/581c9833c2b2f5acf25f140f6b451edc to your computer and use it in GitHub Desktop.
Save kennyxcao/581c9833c2b2f5acf25f140f6b451edc to your computer and use it in GitHub Desktop.
// O(n) space complexity solution
var kthSmallest = function(root, k) {
var stack = [];
var count = 0;
var node = root;
while (node) {
stack.push(node);
node = node.left;
}
while (stack.length > 0) {
node = stack.pop();
count++;
if (count === k) {
return node.val;
}
node = node.right;
while (node) {
stack.push(node);
node = node.left;
}
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment