Created
October 16, 2017 16:59
-
-
Save kennyxcao/581c9833c2b2f5acf25f140f6b451edc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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