题解 | #二叉搜索树的第k个节点#

二叉搜索树的第k个节点

https://www.nowcoder.com/practice/57aa0bab91884a10b5136ca2c087f8ff

void insert(struct TreeNode* root,int a[],int *x){
    if(root->left!=NULL) insert(root->left,a,x);
    a[*x]=root->val;
    (*x)++;
    if(root->right!=NULL) insert(root->right,a,x);
}


int KthNode(struct TreeNode* proot, int k ) {
    // write code here
    if(proot==NULL) return -1;
    int a[1000];
    int x=1;
    for(int i=0;i<1000;i++) a[i]=-1;
    insert(proot,a,&x);
    if(a[k]==-1) return -1;
    else return a[k];
}

全部评论

相关推荐

码农索隆:想看offer细节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务