很简单的中序遍历,代码如下:
//中序遍历
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
DFS(root, k);
return res;
}
void DFS(TreeNode* root, int k) {
if (root == NULL)
return;
DFS(root->left, k);
num++;
if (k == num)
{
res = root->val;
return;
}
DFS(root->right, k);
}
private:
int res = 0;
int num = 0;
};
最后
以上就是精明纸鹤最近收集整理的关于简单题(二叉搜索树中第k小的元素)的全部内容,更多相关简单题(二叉搜索树中第k小内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复