二叉搜索树的第 k 个结点

//思路:
//利用二次搜索树的中序遍历性质

private TreeNode res;
private int cnt;

TreeNode KthNode(TreeNode pRoot, int k) {
    if(pRoot==null){
        return null;
    }
    inOrder(pRoot,k);
    return res;
}

private void inOrder(TreeNode pRoot,int k){
    if(pRoot==null){
        return;
    }
    inOrder(pRoot.left,k);
    cnt++;
    if(cnt==k){
        res = pRoot;
        return;
    }
    inOrder(pRoot.right,k);
}

https://www.mianshi.onlinehttps://www.i9code.cn

本文由博客一文多发平台 OpenWrite 发布!

逃跑的眼镜_bvbEK5
7 声望0 粉丝