63:二叉搜索树的第k个结点

本文介绍了一种算法,用于在给定的二叉搜索树中找到第K大的结点。通过递归中序遍历的方式,可以有效地找到目标结点,并给出一个具体的例子来展示这一过程。
/**
 * 面试题63:二叉搜索树的第k个结点
 * 给定一颗二叉搜索树,请找出其中的第k大的结点
 * 例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
 */
public class _63_binary_k_num {
    public static void main(String[] args) {
        int[] a={8,6,10,5,7,9,11};
        TreeNode63 treeNode63 = new TreeNode63(8);
        treeNode63.left= new TreeNode63(6);
        treeNode63.right= new TreeNode63(10);
        treeNode63.left.left= new TreeNode63(5);
        treeNode63.left.right= new TreeNode63(7);
        treeNode63.right.left= new TreeNode63(9);
        treeNode63.right.right= new TreeNode63(11);

        Solution63 solution63 = new Solution63();
        TreeNode63 treeNode631 = solution63.KthNode(treeNode63, 3);
        System.out.println(treeNode631.val);
    }
}
class Solution63 {
    int k;
    TreeNode63 KthNode(TreeNode63 pRoot, int k) {
        if(pRoot==null||k==0){
            return null;
        }
        this.k=k;
        return getKNum(pRoot);
    }
    public TreeNode63 getKNum(TreeNode63 pRoot){
        TreeNode63 result=null;
        if(pRoot!=null){
            if((result=getKNum(pRoot.left))!=null){
                System.out.println("上"+result.val);
                return result;
            }
            if((k--)==1){
                System.out.println("z"+pRoot.val);
                return pRoot;
            }
            if((result=getKNum(pRoot.right))!=null){
                System.out.println("x"+result.val);
                return result;
            }
        }
        return null;
    }
}
class TreeNode63 {
    int val = 0;
    TreeNode63 left = null;
    TreeNode63 right = null;
    public TreeNode63(int val) {
        this.val = val;
    }
}

转载于:https://www.cnblogs.com/andy-zhou/p/6554357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值