链接:
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
搜索二叉树的特点:
1.左节点比根节点小,右节点比根节点大
2.它的中序遍历是有序的,从小到大
描述:
示例:
限制:
则一定有结果
代码:
class Solution {
public:
int kthLargest(TreeNode* root, int k) {
stack<TreeNode*> stk;
while(root||!stk.empty()){
while(root){
stk.push(root);
root=root->right;
}
root=stk.top();
stk.pop();
k--;
if(k==0) return root->val;
root=root->left;
}
return -1;
}
};