Kth Smallest Element in a BST LeetCode solution in JavaScript
1/**2* Definition for a binary tree node.3* function TreeNode(val, left, right) {4* this.val = (val===undefined ? 0 : val)5* this.left = (left===undefined ? null : left)6* this.right = (right===undefined ? null : right)7* }8*/9/**10* @param {TreeNode} root11* @param {number} k12* @return {number}13*/14var kthSmallest = function (root, k) {15let stack = [];16while (root != null || stack.length > 0) {17while (root != null) {18stack.push(root);19root = root.left;20}21root = stack.pop();22if (--k == 0) break;23root = root.right;24}2526return root.val;27};