Second largest element in BST
Given a Binary Search Tree(BST), find the second largest element.
Examples no 1 (Approach no 1)
Input: Root of below BST
Input: Root of below BST
Source: Microsoft Interview
The idea is similar to below post.
K’th Largest Element in BST when modification to BST is not allowed
The second largest element is second last element in inorder traversal and second element in reverse inorder traversal. We traverse given Binary Search Tree in reverse inorder and keep track of counts of nodes visited. Once the count becomes 2, we print the node.
Below is the implementation of above idea.
2nd largest element is 70
Time complexity : O(h) where h is height of BST.
Space Complexity: O(h) for call stack where h is height ofBST
Example no 2 (Approach no 2):
Input : The Given BST 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 Find the 2nd largest number in given BST?
Program: (Implementation by using while loop)
Second largest value: 13
The code above is a C++ implementation of an algorithm to find the second largest element in a binary search tree (BST).
- The Node struct defines the structure of a node in the binary tree, which has a value, and pointers to its left and right child nodes.
- The findSecondLargest function takes the root node of the BST as its input and returns the second largest value stored in the tree. The function implements the algorithm described in a previous answer, which takes advantage of the property of BSTs that the largest element is stored in the rightmost node. By traversing the right subtree, we reach the largest element, and if it has a left subtree, the second largest element will be stored in the rightmost node of that subtree.
- The function starts by initializing two pointers, current and parent, to the root node of the BST and nullptr, respectively.
- The algorithm then enters a while loop, where current is updated to current->right and parent is updated to current until current->right is nullptr. This loop moves current to the rightmost node in the BST, which is the largest element.
- After the loop, the code checks if current->left is nullptr or not. If current->left is not nullptr, the function enters another while loop, where current is updated to current->right until current->right is nullptr. This loop moves current to the rightmost node in the left subtree of the largest element, which is the second largest element. The value stored in this node is then returned.
- If current->left is nullptr, the second largest element is the parent of the largest element, which is stored in parent. The value stored in parent is then returned.
- Finally, the main function demonstrates how to use the findSecondLargest function by creating a sample BST and calling the function with its root node as input. The output of the function is then printed to the console.
This article is contributed by Ravi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...