二叉搜索树的第 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.online,https://www.i9code.cn
本文由博客一文多发平台 OpenWrite 发布!
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论