剑指Offer-题54(Java版):二叉搜索树的第k个结点

参考自:《剑指Offer——名企面试官精讲典型编程题》

题目:二叉搜索树的第k个结点
给定一棵二叉搜索树,按节点值从小到大排列,找出其中的第k(k≥1)个结点。

主要思路:使用中序遍历,每遍历一个节点时,判断k的值,若k等于1,则当前遍历到的节点就是所求节点,否则使k减1。

关键点:中序遍历

时间复杂度:O(n)

public class KthNodeOfTree {
    private static TreeNode result;
    private static int runK;

    public static void main(String[] args) {
        //            10
        //         /      \
        //        6        14
        //       /\        /\
        //      4  8     12  16
        TreeNode root = TreeNode.generateBinaryTree();
        //4
        System.out.println(findKthNode(root, 1).val);
        //8
        System.out.println(findKthNode(root, 3).val);
        //12
        System.out.println(findKthNode(root, 5).val);
        //System.out.println(findKthNodeByLoop(root, 5).val);  //12
    }

    private static TreeNode findKthNode(TreeNode root, int k) {
        if (root == null || k == 0) {
            return null;
        }
        result = null;
        runK = k;
        findKthNode(root);
        return result;
    }

    //递归
    private static void findKthNode(TreeNode root) {
        if (root == null || result != null) {
            return;
        }
        //中序遍历,先遍历左节点
        findKthNode(root.left);
        if (result == null) {
            if (runK == 1) {
                result = root;
                return;
            }
            runK--;
        }
        //遍历右节点
        findKthNode(root.right);
    }

    //循环
    private static TreeNode findKthNodeByLoop(TreeNode root, int k) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            if (!stack.isEmpty()) {
                cur = stack.pop();
                if (k == 1) {
                    return cur;
                }
                k--;
                cur = cur.right;
            }
        }
        return null;
    }
}

class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

    //            10
    //         /      \
    //        6        14
    //       /\        /\
    //      4  8     12  16

    /**
     * 生成二叉搜索树
     *
     * @return
     */
    public static TreeNode generateBinaryTree() {
        TreeNode root = new TreeNode(10);
        TreeNode node6 = new TreeNode(6);
        TreeNode node14 = new TreeNode(14);
        TreeNode node4 = new TreeNode(4);
        TreeNode node8 = new TreeNode(8);
        TreeNode node12 = new TreeNode(12);
        TreeNode node16 = new TreeNode(16);
        connectNode(root, node6, node14);
        connectNode(node6, node4, node8);
        connectNode(node14, node12, node16);
        return root;
    }

    public static void connectNode(TreeNode root, TreeNode left, TreeNode right) {
        root.left = left;
        root.right = right;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值