题目:
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& result){ //本题利用二叉搜索树的排序性质
if(cur==nullptr) return;
traversal(cur->right, result); //唯一要注意的是题目要求第k大的,所以要把大的放在前面。遍历顺序右、中、左
result.push_back(cur->val);
traversal(cur->left, result);
}
int kthLargest(TreeNode* root, int k) {
vector<int> result;
traversal(root, result);
return result[k-1];
}
};
标签:TreeNode,cur,Offer,int,54,traversal,result,二叉
From: https://www.cnblogs.com/fly-smart/p/17626429.html