Created
April 3, 2017 16:59
-
-
Save mikeliao97/6fde41781cebc1351b35a922a27fb535 to your computer and use it in GitHub Desktop.
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
/** | |
* 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