Kth Smallest Element in a BST (Leetcode 230) [Optimisation Required]

Problem Link: https://leetcode.com/problems/kth-smallest-element-in-a-bst/

class Solution:

    def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
        
        def fun(root):
            if(root is None):
                return
            fun(root.left)
            ans.append(root.val)
            fun(root.right)
            
        ans=[]
        fun(root)
        return ans[k-1]

Last updated