Find K-th Smallest and Largest Element
Properties and Validation DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Find the K-th Smallest and K-th Largest Element in a Binary Search Tree (BST), Binary Search Tree, Tree Traversal, In-order Traversal, Recursion, Time Complexity, Space Complexity, Arrays, Brute Force, Optimization, inorder traversal, space complexity, algorithm comparison, divide and conquer, binary search tree, time complexity analysis, tree traversal, Kth Smallest/Largest in BST, Validate BST.
Given a Binary Search Tree (BST) and an integer K, find and return the K th smallest and K th largest element in the given BST. Input Specification: The first line of input represents the Binary Search Tree. The tree is represented in a level order fashion. Each node's value is an integer. 1 denotes a null node. The second line contains a single integer K. Output Specification: Output two integers, the K th smallest element and the K th largest element, separated by a space. Example 1: Example 2: