# Kth Smallest Element in a BST ###### tags: `Medium` `Tree` >question : https://leetcode.com/explore/interview/card/top-interview-questions-medium/108/trees-and-graphs/790/ >reference : >related problem : ## My Solution ```java= /** * 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 { private Stack<TreeNode> stack; public int kthSmallest(TreeNode root, int k) { stack = new Stack<TreeNode>(); toTheLastLeftNode(root); TreeNode ans = new TreeNode(); for(int i = 0; i < k; i++) { ans = stack.pop(); if(ans.right != null) toTheLastLeftNode(ans.right); } return ans.val; } private void toTheLastLeftNode(TreeNode node) { while(node != null) { stack.add(node); node = node.left; } } } ``` ## Other Solution ## K-th largest in BST ![](https://i.imgur.com/Ra4PHGw.png)