方法:利用递归
先右节点 在中间节点 最后左节点
注意:count会变换 因此 函数入参应该是int &count
class Solution {
int res = 0;
void find(TreeNode * root, int k, int &count)
{
if(!root) return;
find(root->right, k, count);
count++;
if(count == k)
{
res = root->val;
return;
}
find(root->left, k, count);
}
public:
int kthLargest(TreeNode* root, int k) {
int count = 0;
find(root, k, count);
return res;
}
};