# 【Leetcode】Find the kth in the 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.

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.

【思路】

BST的特点就是排序性，左边的节点一定比root小，右边的节点一定比root大。

k - left,就变成了新的k.

【代码】

Binary search:

public int kthSmallest(TreeNode root, int k) {
int count = countNodes(root.left);
if (k <= count) {
return kthSmallest(root.left, k);
<span style="background-color: rgb(255, 255, 102);">} else if (k > count + 1) {
return kthSmallest(root.right, k-1-count); // 1 is counted as current node</span>
}

return root.val;
}

public int countNodes(TreeNode n) {
if (n == null) return 0;

return 1 + countNodes(n.left) + countNodes(n.right);
}

DFS：

  // better keep these two variables in a wrapper class
private static int number = 0;
private static int count = 0;

public int kthSmallest(TreeNode root, int k) {
count = k;
helper(root);
return number;
}

public void helper(TreeNode n) {
if (n.left != null) helper(n.left);
count--;
if (count == 0) {
number = n.val;
return;
}
if (n.right != null) helper(n.right);
}

BFS :

 public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> st = new Stack<>();

while (root != null) {
st.push(root);
root = root.left;
}

while (k != 0) {
TreeNode n = st.pop();
k--;
if (k == 0) return n.val;
TreeNode right = n.right;
while (right != null) {
st.push(right);
right = right.left;
}
}

return -1; // never hit if k is valid
}

• 本文已收录于以下专栏：

## 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. Not...
• sunao2002002
• 2015年07月02日 14:27
• 5618

## leetcode 230: Kth Smallest Element in a BST

leetcode 230: Kth Smallest Element in a BST python java c++
• xudli
• 2015年07月07日 06:14
• 9369

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

• SunliyMonkey
• 2015年09月02日 15:34
• 1331

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

• happyaaaaaaaaaaa
• 2016年05月24日 11:17
• 1098

## Find kth number in a BST

Find kth number in a BST struct Node { Node *left; Node *right; int val; Node(int v) : val(v), ...
• brucehb
• 2017年02月22日 01:20
• 176

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

• jmspan
• 2016年04月05日 11:16
• 641

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

230. Kth Smallest Element in a BST My Submissions Question Total Accepted: 35869 Total Su...
• bruce128
• 2016年02月16日 15:27
• 1554

## [LeetCode] 4 Find the kth smallest element in two sorted array

Find the kth smallest element in two sorted array public int kthSmallestElem(int[] num1,int[] num2,i...
• q476855396
• 2015年07月12日 20:03
• 368

## LeetCode230——Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
• booirror
• 2015年08月03日 14:25
• 767

## 【特别重要!!!快排的partition思想】【leetcode题解】【M】【71】Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so...
• sscssz
• 2015年12月13日 20:10
• 675

举报原因： 您举报文章：【Leetcode】Find the kth in the BST 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)