题目地址
LeetCode#230 Kth Smallest Element in a BST
题目描述
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
解题思路
题目要求找到树种第K个最小的值,可以将树的节点值保存到容器数组中,然后排序就可以了。emm
解题代码【.CPP】
1 | /** |