Find the smallest element in a BST

Leetcode question

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Solution

/**
 * 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) {
    var count = 0;
    var isFound = false;
    var res = null;
    inorder(root);
    return res;

    function inorder(node){
        if(node !== null && !isFound){
            inorder(node.left);
            count++;
            if(count === k){
                res = node.val;
                isFound = true;
                return;
            }
            inorder(node.right);
        }
    }
};