找第k大的专题: 378. Kth Smallest Element in a Sorted Matrix && 230. Kth Smallest Element in a BST

本文探讨了在排序矩阵和二叉搜索树中寻找第k小元素的算法,包括使用堆排序和二叉搜索树的中序遍历来解决问题的详细方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

找第k大的专题

378. Kth Smallest Element in a Sorted Matrix

1. 题目描述
题目链接

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.

2. 题目分析
同一行中,从左到右是升序,同一列中,从上到下是升序,但有个坑,就是不保证上一行的结尾的数值会比下一行的第一个数值小,所以说到底对于找第k小的值这二维数组是没有规律的。

3. 解决思路
那么如果我们要找第k小的值,那么肯定是需要排序的。排序可以使用堆排序找到第k小的值,或者直接调用java中的数组排序API,即Arrays.sort(array);

4. 代码实现(java)

  • 堆排序,可以使用优先队列,会自动进行堆排序
public static int kthSmallest(int[][] matrix, int k) {
        if (matrix == null){
            return 0;
        }
        PriorityQueue<Integer> priorityQueue =new PriorityQueue<Integer>(matrix.length * matrix.length);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                priorityQueue.add(matrix[i][j]);
            }
        }
        int count = 0;
        while (!priorityQueue.isEmpty()){
            int value = priorityQueue.poll();
            count++;
            if (count == k){
                return value;
            }
        }
        return 0;
    }
  • Arrays.sort(array)排序
public static int kthSmallest(int[][] matrix, int k) {
        if (matrix == null){
            return 0;
        }
        int[] dp = new int[matrix.length * matrix.length];
        int index = 0;
        for(int i=0; i<matrix.length; i++) {
            for(int j=0; j<matrix.length; j++) {
                dp[index] = matrix[i][j];
                index++;
            }
        }
        Arrays.sort(dp);
        return dp[k-1];
    }

230. Kth Smallest Element in a BST

1. 题目描述
题目链接

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
Example 1:
Input: root = [3,1,4,null,2], k = 1
3
/
1 4

2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/
3 6
/
2 4
/
1
Output: 3
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

2. 题目分析
从二叉搜索树中找第k小的值,要找第k小的值,那么肯定要排序,我们知道,二叉搜索树的中序遍历就是有序列表了,所以我们只要对BST进行中序遍历就行了。
中序遍历的方式有两种:递归、非递归(栈)

3. 代码实现(java)
这里就不分析代码过程了,如果对BST三种遍历不了解的,可以查看我的博客:
145. 二叉树的后序遍历145. 二叉树的中序遍历145. 二叉树的先序遍历

/**
     * 230. Kth Smallest Element in a BST
     * @param root
     * @param k
     * @return
     */
    public int kthSmallest(TreeNode root, int k) {
        if (root == null){
            return 0;
        }
        int count = 0;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty()){
            //找到最左叶子节点
            if (node != null){
                stack.push(node);
                node = node.left;
            }else{
                node = stack.pop();
                count++;
                System.out.println("节点值为:"+node.val);
                if (count == k){
                    return node.val;
                }
                node = node.right;
            }
        }
        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值