Medium Data Structures Trees & Binary Trees
How do you find the kth smallest element in a BST?
Answer
Use in-order traversal which visits BST nodes in sorted order. Iterative approach with stack: go left as far as possible while pushing nodes, pop and decrement k, if k is 0 return current value, else go right. O(H + k) time where H is height. Alternative: augment BST nodes with subtree sizes for O(H) time by comparing k with left subtree size to decide direction.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer