需求
给定一棵二叉搜索树,求出第k大的节点。
思路
二叉 搜索树又叫二叉平衡树,左子树小于根结点,右子树大于根结点。所以中序遍历二叉搜索树的话会是从小到大排序。
实现
typedef struct node{
int data;
struct node *left;
struct node *right;
}BTNode;
BTNode* searchTagCore(BTNode *root, int* k){
BTNode *tag = NULL;
if (root->left){
tag = searchTagCore(root->left, k);
}
if (!tag){
if (1 == *k){
tag = root;
}
(*k)--;
}
if (!tag&&root->right){
tag = searchTagCore(root->right,k);
}
return tag;
}
BTNode* searchTag(BTNode *root, int k){
if (!root || !k){
return NULL;
}
return searchTagCore(root, &k);
}
1万+



被折叠的 条评论
为什么被折叠?



