public class E54KthBSTNode {
//二叉搜索树中第k个节点
private static int index;
public static BinaryTreeNode getKthNode(BinaryTreeNode root, int k){
if (root == null)
return null;
index = k;
return getKthNodeCore(root);
}
private static BinaryTreeNode getKthNodeCore(BinaryTreeNode root) {
//采用二叉树的中序遍历顺序查找第k个节点
BinaryTreeNode target = null;
if (root.left != null)
target = getKthNodeCore(root.left);
if (target == null){
if (index == 1)
target = root;
index--;
}
if (target == null && root.right != null)
target = getKthNodeCore(root.right);
return target;
}
}
最后
以上就是曾经宝贝最近收集整理的关于二叉搜索树的第K大节点(Java实现)的全部内容,更多相关二叉搜索树内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复