Skip to content

Instantly share code, notes, and snippets.

@mikeliao97
Created April 3, 2017 16:59
Show Gist options
  • Save mikeliao97/6fde41781cebc1351b35a922a27fb535 to your computer and use it in GitHub Desktop.
Save mikeliao97/6fde41781cebc1351b35a922a27fb535 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) {
if (k === 1 && root) {
return root.val
} else if (!root.left && !root.right && k > 1) {
return null;
} else {
return kthSmallest(root.left, k - 1) || kthSmallest(root.right, k - 3);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment