代码拉取完成,页面将自动刷新
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* 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'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?
*/
public class _230 {
public static class Solution1 {
public int kthSmallest(TreeNode root, int k) {
List<Integer> inorderList = new ArrayList<>();
inorder(root, inorderList);
return inorderList.get(k - 1);
}
private void inorder(TreeNode root, List<Integer> inorderList) {
if (root == null) {
return;
}
if (root.left != null) {
inorder(root.left, inorderList);
}
inorderList.add(root.val);
if (root.right != null) {
inorder(root.right, inorderList);
}
return;
}
}
public static class Solution2 {
/**
* Inorder traversal gives the natural ordering of a BST, no need to sort.
*/
int count = 0;
int result = Integer.MIN_VALUE;
public int kthSmallest(TreeNode root, int k) {
inorder(root, k);
return result;
}
private void inorder(TreeNode root, int k) {
if (root == null) {
return;
}
inorder(root.left, k);
count++;
if (count == k) {
result = root.val;
return;
}
inorder(root.right, k);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。