230.二叉搜索树中第k小的元素
/*** 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}*/
var kthSmallest = function(root, k) {let stack=[]let node=rootwhile(node||stack.length){while(node){stack.push(node)node=node.left}node=stack.pop()if(--k==0){return node.val}node=node.right}return false
};
采用中序遍历 找到第k个值