一、题目
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
二、题目大意
找出二叉搜索树中第K小的元素。
三、解题思路
中序遍历二叉搜索树。
四、代码实现
const kthSmallest1 = (root, k) => {
const ans = []
help(root)
return ans[k - 1]
function help (root) {
if (!root) {
return
}
help(root.left)
ans.push(root.val)
help(root.right)
}
}
如果本文对您有帮助,欢迎关注微信公众号,为您推送更多内容,ε=ε=ε=┏(゜ロ゜;)┛。