Code:
js
import {
TreeNode,
ArrayToTree
} from '../common.js'
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function(root, k) {
const stack = []
while(root || stack.length) {
while(root) {
stack.push(root)
root = root.left
}
root = stack.pop()
if (--k === 0) {
return root.val
}
root = root.right
}
};
console.log(kthSmallest(ArrayToTree([5,3,6,2,4,null,null,1]), 3))