// 题目描述
// 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
int count =0;
TreeNode KthNode(TreeNode pRoot, int k) {
if (pRoot==null){
return pRoot;
}
if (k<=0){
return null;
}
this.count = k;
return doKthNode(pRoot);
}
TreeNode doKthNode(TreeNode pRoot){
TreeNode treeNode = null;
if (pRoot!=null){
if ((treeNode=doKthNode(pRoot.left))!=null) {
return treeNode;
}
if (--count==0){
return pRoot;
}
if ((treeNode=doKthNode(pRoot.right))!=null) {
return treeNode;
}
}
return null;
}