剑指 Offer 54. 二叉搜索树的第k大节点
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int count=0;
int i=0;
int res=0;
public int kthLargest(TreeNode root, int k) {
count(root);
finds(root,k);
return res;
}
public void count(TreeNode root){
if(root==null) return ;
count(root.left);
count++;
count(root.right);
}
public void finds(TreeNode root,int k){
if(root==null) return ;
finds(root.left,k);
i++;
if(i==count-k+1){
res= root.val;
}
finds(root.right,k);
}
}