9月 7 2017 Online Judge►LeetCode [LeetCode] 230 - Kth Smallest Element in a BST 題意在 Binary Search Tree 中找到第 K 小的元素。 解法Inorder Traversal 後輸出第 K 個元素即可。 程式1234567891011121314151617181920212223242526272829/** * 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) { let stack = []; let result = []; let node = root; while( stack.length !== 0 || node !== null ){ while ( node !== null ){ stack.push(node); node = node.left; } node = stack.pop(); result.push(node.val); node = node.right; } return result[k-1];}; Newer [LeetCode] 144 - Binary Tree Preorder Traversal Older [LeetCode] 287 - Find the Duplicate Number