Day 85 of 100 Days LeetCode Challenge

LeetCode Challenge #230. Kth Smallest Element in a BST

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

 

Example 1:

kthtree1

Input: root = [3,1,4,null,2], k = 1
Output: 1

Example 2:

kthtree2

Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3

 

Constraints:

  • The number of nodes in the tree is n.
  • 1 <= k <= n <= 104
  • 0 <= Node.val <= 104
Video Solution
Java Solution
				
					class Solution {
    int count = 0 ;
    int ans = 0 ;
    public int kthSmallest(TreeNode root, int k) {
        helper(root,k);
        return ans ;
    }

    public void helper(TreeNode root , int k){
        if(root == null){
            return;
        }

        helper(root.left , k);
        count = count +1 ;
        if(count == k){
            ans = root.val;
        }
        helper(root.right,k);
    }
}
				
			

Happy Coding with edSlash