【LeetCode】 230. Kth Smallest Element in a BST
2017-01-10 00:31
369 查看
Given a binary search tree, write a function
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:
Try to utilize the property of a BST.
What if you could modify the BST node's structure?
The optimal runtime complexity is O(height of BST).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int kthSmallest(TreeNode root, int k) {
int count = countNode(root.left);
if (k <= count) {
return kthSmallest(root.left, k);
} else if (k > count + 1) {
return kthSmallest(root.right, k - count - 1);
}
return root.val;
}
private int countNode(TreeNode node) {
if (node == null) {
return 0;
}
return 1 + countNode(node.left) + countNode(node.right);
}
}
kthSmallestto 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:
Try to utilize the property of a BST.
What if you could modify the BST node's structure?
The optimal runtime complexity is O(height of BST).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int kthSmallest(TreeNode root, int k) {
int count = countNode(root.left);
if (k <= count) {
return kthSmallest(root.left, k);
} else if (k > count + 1) {
return kthSmallest(root.right, k - count - 1);
}
return root.val;
}
private int countNode(TreeNode node) {
if (node == null) {
return 0;
}
return 1 + countNode(node.left) + countNode(node.right);
}
}
相关文章推荐
- 【Leetcode】:230. Kth Smallest Element in a BST 问题 in JAVA
- [LeetCode]230. Kth Smallest Element in a BST
- Leetcode 230. Kth Smallest Element in a BST
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
- LeetCode 230. Kth Smallest Element in a BST
- [LeetCode]230. Kth Smallest Element in a BST
- leetcode-230. Kth Smallest Element in a BST
- 【leetcode】230. Kth Smallest Element in a BST
- leetcode-java-230. Kth Smallest Element in a BST
- 【leetcode】230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST LeetCode
- LeetCode 230. Kth Smallest Element in a BST
- [leetcode] 230. Kth Smallest Element in a BST 解题报告
- Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)
- LeetCode 230. Kth Smallest Element in a BST 解题报告
- LeetCode 230. Kth Smallest Element in a BST
- leetcode 230. Kth Smallest Element in a BST
- LeetCode *** 230. Kth Smallest Element in a BST
- [LeetCode] 230. Kth Smallest Element in a BST 解题思路
- [leetcode] 230. Kth Smallest Element in a BST