Question
Which of the following techniques is most efficient for
finding the kth smallest element in a Binary Search Tree (BST)?Solution
In a Binary Search Tree (BST), an Inorder Traversal retrieves elements in sorted order. To find the kth smallest element, an efficient approach is to perform an Inorder Traversal and stop after visiting the kth element. This method is efficient because it directly leverages the BST's inherent properties without extra data structures. Steps:
- Perform a recursive Inorder Traversal.
- Maintain a counter to track the number of visited nodes.
- When the counter equals k , return the current node's value.
A train started from station P and preceded towards station Q at a speed of 90 km/h. 54 minutes later, another train started from station Q and preceded...
Length of a train, whose speed is 60 km/h, is 300 metres. Find the time taken (in seconds) by train to cross a bridge of length 600 metres.
- Train 'X' departed from Chennai to Hyderabad at 7:00 a.m. Train 'Y' left Hyderabad for Chennai at 9:00 a.m., traveling at 65 km/h. They crossed each other ...
A 250-meter-long train crosses a 150-meter-long train coming from the opposite direction in 20 seconds. If the speed of the first train is 45 km/h, then...
Train 'A' crosses a pole in 35 seconds and takes 40 seconds to cross a platform that is 120 meters long. Train 'B' is half the length of Train 'A' and t...
The ratio of the length of two trains P and Q is 5:6 and the ratio of the time taken by both trains to cross a man standing on a platform is 2:3. If the...
Two trains A and B, were proceeding in the same direction on parallel tracks at 23 km/hr and 77 km/hr respectively. A man noticed that it took exactly 1...
Speeds of train 'A' and train 'B' are in the ratio 4:5, respectively. Train 'B' can completely overtake train 'A' in 180 seconds. If lengths of train 'A...
Train A running at a speed of 48 km/hr crosses train B in 20 seconds. Find the speed of train B (in km/hr), if the sum of the length of train A and B is...
A train traveling at 43.2 km/h takes 'm' seconds to pass a pole. What is the length of the train in meters?