//给定一颗二叉搜索树,请找出其中的第K大的结点。
#include "iostream"
struct treeNode
{
treeNode* left;
treeNode* right;
int value;
};
treeNode* kthNode(treeNode* pRoot, int k, int *ith)
{
if (pRoot == NULL)
return NULL;
kthNode(pRoot->left, k, ith);
*ith++;
if (*ith == k)
return pRoot;
kthNode(pRoot->right, k, ith);
}
treeNode *findKthNode(treeNode* pRoot, int k)
{
if (pRoot == NULL || k <= 0)
return NULL;
int ith = 0;
return kthNode(pRoot, k, &ith);
}
中序遍历。