关闭

【Leetcode】Kth Smallest Element in a BST

382人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-bst/

题目:

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.

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?

Hint:

  1. Try to utilize the property of a BST.
  2. What if you could modify the BST node's structure?
  3. The optimal runtime complexity is O(height of BST).
思路:
1、BST中序遍历的结果是对所有结点值逐渐增大的一个排列。用List保存遍历结果,返回List的第k个元素就行了。时间复杂度是O(n),空间复杂度是O(n)。
2、中序遍历,用计数器记录当前遍历的结点个数,当遍历到k个元素的时候返回该结点的val,无需保存遍历的结点,时间复杂度O(n),空间复杂度O(1)。
3、修改结点数据结构,在TreeNode结点加入leftCount,表示左孩子数目,根据结点的leftCount调整递归分支,它不会遍历所有结点,而是自上而下的路径,所以时间复杂度是O(height of BST)。
算法1:
	public int kthSmallest(TreeNode root, int k) {
		List<Integer> list = new ArrayList<Integer>();
		list = inOrder(root, list);
		return list.get(k - 1);
	}

	public List<Integer> inOrder(TreeNode p, List<Integer> order) {
		if (p != null) {
			order = inOrder(p.left, order);
			order.add(p.val);
			order = inOrder(p.right, order);
		}
		return order;
	}


算法2:
	int count = 0, result;

	public int kthSmallest(TreeNode root, int k) {
		if (root != null) {
			kthSmallest(root.left, k);
			count++;
			if (count == k) {
				result = root.val;
			}
			kthSmallest(root.right, k);
		}
		return result;
	}


算法3:
class newTreeNode { // BST
	newTreeNode left, right;
	int val, leftCount;// 记录左孩子个数

	newTreeNode(int val) {
		this.val = val;
	}
}
	int result;

	public int kthSmallest(newTreeNode root, int k) {
		if (root != null) {
			if (root.leftCount + 1 == k) {
				result = root.val;
			} else if (root.leftCount + 1 < k) {
				kthSmallest(root.right, k - (root.leftCount + 1));
			} else {
				kthSmallest(root.left, k);
			}
		}
		return result;
	}



0
0
查看评论

LeetCode 230: Kth Smallest Element in a BST

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&...
  • sunao2002002
  • sunao2002002
  • 2015-07-02 14:27
  • 5615

leetcode 230: Kth Smallest Element in a BST

leetcode 230: Kth Smallest Element in a BST python java c++
  • xudli
  • xudli
  • 2015-07-07 06:14
  • 9367

LeetCode 230: Kth Smallest Element in a BST 查找二叉排序树

博客转载请注明地址:题目描述题目地址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 在一颗二叉排序树当中,寻找第k大的数。考察点 递归 树的遍历 陷阱无Code/** * Definition for a binary ...
  • SunliyMonkey
  • SunliyMonkey
  • 2015-09-02 15:34
  • 1328

leetcode 230. Kth Smallest Element in a BST-递归|非递归

原题链接:230. Kth Smallest Element in a BST 【思路-Java、Python】——递归实现 我们知道二分查找数(BST)的性质——任何一个节点的值均大于左子树的任意节点值,而小于右子树的任一节点值。那么这样就可以知道最小值的一个节点在树的最左端,最大值的一个节点在树...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-05-24 11:17
  • 1098

LeetCode 230. Kth Smallest Element in a BST 解题报告

230. Kth Smallest Element in a BST My Submissions Question Total Accepted: 35869 Total Submissions: 100057 Difficulty:&...
  • bruce128
  • bruce128
  • 2016-02-16 15:27
  • 1553

LeetCode 230. Kth Smallest Element in a BST(二叉搜索树中的第K个元素)

原题网址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Given a binary search tree, write a function kthSmallest to find the...
  • jmspan
  • jmspan
  • 2016-04-05 11:16
  • 640

LeetCode—378. Kth Smallest Element in a Sorted Matrix

Kth Smallest Element in a Sorted Matrix思路:1.首先这题把所有的数组遍历出来然后直接排序就可以AC..... 2.正常思路就是维系最大堆。 GitHub地址:https://github.com/corpsepiges/leetcode ...
  • corpsepiges
  • corpsepiges
  • 2016-08-02 10:20
  • 2155

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告
  • camellhf
  • camellhf
  • 2016-10-07 23:29
  • 344

[leetcode] 378. Kth Smallest Element in a Sorted Matrix 解题报告

题目链接: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ Given a n x n matrix where each of the rows an...
  • qq508618087
  • qq508618087
  • 2016-08-20 15:43
  • 1414

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows ...
  • yeqiuzs
  • yeqiuzs
  • 2016-08-01 21:54
  • 3314
    个人资料
    • 访问:569071次
    • 积分:8678
    • 等级:
    • 排名:第2674名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论