/**
* 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}
*/
function Track() {
this.val;
this.cnt = 0;
}
let track;
var kthSmallest = function (root, k) {
track = new Track();
find(root, k);
return track.val;
};
function find(node, k) {
if (node === null) return;
if (track.val !== undefined) return;
find(node.left, k);
track.cnt++;
if (track.cnt === k) {
track.val = node.val;
return;
}
find(node.right, k);
}