/**
* 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) {
ArrayList<Integer> l = inorder(root, new ArrayList<Integer>());
return l.get(k - 1);
}
public ArrayList<Integer> inorder(TreeNode root, ArrayList<Integer> l1) {
if (root == null) return l1;
inorder(root.left, l1);
l1.add(root.val);
inorder(root.right, l1);
return l1;
}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Kth Smallest Element in a BST.
Memory Usage: 39.3 MB, less than 34.14% of Java online submissions for Kth Smallest Element in a BST.
Binary Search Tree니깐 inorder로 찾으면 작 -> 큰으로 찾을 수 있다는 점을 이용해서 풀었다