Javascript with explanation. Recursive calls, short circuit when reach k-th node.
Short and simple, good code documents itself.  Have a look.  \n\nBasically you know that it\'s a binary search tree, so go left for smallest values, after begin
Short and simple, good code documents itself.  Have a look.  \n\nBasically you know that it\'s a binary search tree, so go left for smallest values, after begin adding to array values.  Now that I think about it, I guess we just need a counter, rather then an array of values.  Once we get to the kth value, just return that and done.  No need for the array.  \n\nAnyway, here is what i\'ve got.  This was a normal tree traversal, just added the short circuit factor.\n\nIf this helped you, please upvote so others can read.  Cheers.\n\n```\nvar kthSmallest = function(root, k) {\n  let vals = [];\n  (function dfs(node) {\n    if (vals.length !=k) { //no need to keep going after reach k-th number\n      if(node.left) dfs(node.left); //go left first\n      vals.push(node.val); //finished going left, now start adding values\n      if (node.right) dfs(node.right); //if have right, go there and repeat process\n    }  \n  })(root) // IFFE Immediately Invoking Function Expression, starting from root.\n  return vals[k-1]; //return element, but as i mentioned in the descript, don\'t need this full array, just the k-th elm\n};\n```
['Tree', 'Binary Tree', 'JavaScript']