Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)

5 篇文章 0 订阅

包含了三个问题

1.Kth smallest element in a sorted matrix

2.Kth smallest element in a BST(Binary Search Tree)

3.Kth smallest element in two sorted array

/**
 * 包含了三个问题
 * 1.Kth smallest element in a sorted matrix
 * 2.Kth smallest element in a BST(Binary Search Tree)
 * 3.Kth smallest element in two sorted array
 * @author 健身小码哥
 *
 */
public class KthSmallestElement {
	/**
	 * Kth smallest element in a sorted matrix
	 * 思路一:二分查找
	 * @param matrix
	 * @param k
	 * @return
	 */
	public int findKthSmallestElementInSortedMatrix(int matrix[][] , int k)
	{
		int n = matrix.length;
        	int startValue = matrix[0][0];
        	int endValue = matrix[n-1][n-1];
        
        	while (startValue < endValue) 
       		{
           		int midValue = (startValue + endValue) / 2;
            
			int smallerCount = 0;
            
			for (int i = 0; i < n; i++) 
			{
            			for(int j = 0 ; j < n ; j++)
            			{
            				if(matrix[i][j] <= midValue)
            				{
            					smallerCount++;
            				}
            			}
			}
			if (smallerCount < k) 
			{
				startValue = midValue + 1;
			} else 
			{
				endValue = midValue;    
			}
		}
		return startValue;
	}
	/**
	 * Kth smallest element in a BST(Binary Search Tree)
	 * @param root
	 * @param k
	 * @return
	 */
	public int findKthSmallestElementInBST(TreeNode root , int k)
	{
		if (root == null)
		{
			return -1;  
		}
		int leftSize = getTreeSize(root.left);  
		if (k == leftSize+1)
		{  
		    return root.value;  
		}else if (k <= leftSize)
		{  
		    return findKthElementInBST(root.left,k);  
		}else
		{  
		    return findKthElementInBST(root.right, k-leftSize-1);  
		}  
	}
	/**
	 * 查找两个有序数组中第k小的数
	 * @param nums1			有序数组1
	 * @param startIndex1	数组1开始位置
	 * @param nums2			有序数组2
	 * @param startIndex2	数组2开始位置
	 * @param k				
	 * @return
	 */
	public int findKthSmallestElementInTwoSortArray(int nums1[] ,int startIndex1, int nums2[] , int startIndex2 , int k)
	{
		int m = nums1.length - startIndex1;
		int n = nums2.length - startIndex2;
		//确保数组2的长度较大,方便之后的操作
		if(m > n)
		{
			return findKthSmallestElementInTwoSortArray(nums2, startIndex2, nums1, startIndex1, k);
		}
		
		if(m == 0)
		{
			return nums2[k-1];
		}
		
		if(k <= 1)
		{
			return Math.min(nums1[startIndex1], nums2[startIndex2]);
		}
		
		int minMid = Math.min(k/2,m);
		int maxMid = k - minMid;
		
		if(nums1[minMid + startIndex1 - 1] < nums2[maxMid +startIndex2 -1])
		{
			return findKthSmallestElementInTwoSortArray(nums1, startIndex1+minMid, nums2, startIndex2, k - minMid);
		}else if(nums1[minMid + startIndex1 - 1] > nums2[maxMid+startIndex2 -1])
		{
			return findKthSmallestElementInTwoSortArray(nums1, startIndex1, nums2, startIndex2 + maxMid, k - maxMid);
		}else
		{
			return nums1[minMid+startIndex1 -1];
		}
		
	}
	/**
	 * 
	 * 获取树的大小(节点个数)
	 * @param root	根节点
	 * @return
	 */
	public int getTreeSize(TreeNode root)
	{  
		if (root == null)
		{
		    return 0;
		}
		return 1+getTreeSize(root.left) + getTreeSize(root.right);          
	}  
	
	public static void main(String[] args) {
		int nums1[] = {1,3,5,7,9};
		int nums2[] = {2,4,6,8,10};
		int s = new KthSmallestElement().findKthSmallestElementInTwoSortArray(nums1, 0, nums2, 0, 6);
		System.out.println(s);
	}
	
}
/**
 * 树节点类
 * @author 健身小码哥
 *
 */
class TreeNode
{
	TreeNode left;
	TreeNode right;
	int value;
}







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值