二叉搜索树,自然想到中序遍历,然后得到一个有序数组,然后就可以解决了。
class Solution {
public:
vector<int> res;
void dfs(TreeNode* node){
if (node == NULL) return;
// 中序遍历
dfs(node->left); // 左
res.push_back(node->val); // 中
dfs(node->right); // 右
}
int kthLargest(TreeNode* root, int k) {
dfs(root);
return res[res.size()-k];
}
};
二叉搜索树——剑指 Offer 54. 二叉搜索树的第k大节点
原文:https://www.cnblogs.com/lzyrookie/p/14678635.html