230:二叉搜索树中第K小的元素
This commit is contained in:
parent
9a5f26f13d
commit
1048e70f94
@ -0,0 +1,88 @@
|
||||
//给定一个二叉搜索树的根节点 root ,和一个整数 k ,请你设计一个算法查找其中第 k 个最小元素(从 1 开始计数)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:root = [3,1,4,null,2], k = 1
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:root = [5,3,6,2,4,null,null,1], k = 3
|
||||
//输出:3
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 树中的节点数为 n 。
|
||||
// 1 <= k <= n <= 104
|
||||
// 0 <= Node.val <= 104
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 k 小的值,你将如何优化算法?
|
||||
// Related Topics 树 二分查找
|
||||
// 👍 397 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//230:二叉搜索树中第K小的元素
|
||||
public class KthSmallestElementInABst {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new KthSmallestElementInABst().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
|
||||
public int kthSmallest(TreeNode root, int k) {
|
||||
List<Integer> nums = getList(root, new ArrayList<>());
|
||||
return nums.get(k - 1);
|
||||
}
|
||||
|
||||
private List<Integer> getList(TreeNode root, List<Integer> list) {
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
getList(root.left, list);
|
||||
list.add(root.val);
|
||||
getList(root.right, list);
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
34
src/main/java/leetcode/editor/cn/KthSmallestElementInABst.md
Normal file
34
src/main/java/leetcode/editor/cn/KthSmallestElementInABst.md
Normal file
@ -0,0 +1,34 @@
|
||||
<p>给定一个二叉搜索树的根节点 <code>root</code> ,和一个整数 <code>k</code> ,请你设计一个算法查找其中第 <code>k</code><strong> </strong>个最小元素(从 1 开始计数)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,1,4,null,2], k = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,3,6,2,4,null,null,1], k = 3
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数为 <code>n</code> 。</li>
|
||||
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 <code>k</code> 小的值,你将如何优化算法?</p>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>二分查找</li></div></div>\n<div><li>👍 397</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user