Introduction
Binary Trees are popular Data Structures and have a wide range of applications throughout various fields. Binary Trees are also important when it comes to interviews with top tech companies. A mastery of Binary Trees could help us solve a lot of problems. In this article, we are going to take a look at one such problem.
Binary search tree (BST)
Before jumping to the question, let’s understand what a Binary search tree is:
A Binary search tree (BST) is a type of binary tree in which the nodes are arranged in a specific order. It is also known as an ordered binary tree.
Properties of a Binary search tree (BST):
- The value of all the nodes in the left subtree of a node is smaller than the node’s value.
- All the nodes in the right subtree of a node are greater than the node’s value.
- All the root node’s left and right subtrees must follow the above properties.
Example:
Problem statement
We are given a Binary search tree. Our task is to find the sum and the product of the maximum and minimum elements of the binary search tree.
Example:
Input1:
Output1: Sum = 30
Product = 189
Explanation:
The maximum element = 21, and the minimum element = 9,
Sum = 21 + 9 = 30
Product = 21 * 9 = 189
Input2:
Output2: Sum = 88
Product = 1207
Explanation:
The maximum element = 71 and the minimum element = 17,
Sum = 71 + 17 = 88
Product = 71 * 17 = 1207
Approach
The idea is simple, find the minimum and the maximum value in the binary search tree. After that, find the required sum and the product of the minimum and maximum elements.
In a Binary search tree (BST), the left child of each node must be less than its parent, and the right child must be greater than its parent node.
Because of its ordered structure, finding the maximum and the minimum value in the Binary search tree is a simple operation.
So, if we consider the root node of the Binary search tree (BST), the left subtree of the binary search tree must have nodes with values less than or equal to the root node, and the right subtree must have nodes with values greater than the root node.
So, the node with minimum and maximum value in the binary search tree is the tree’s leftmost and rightmost node, respectively.
Steps for finding the minimum element in the Binary search tree:
- Declare a curr pointer pointing to the root of the Binary search tree.
- Run a while loop till curr -> left != NULL and do curr = curr -> left inside the while loop.
-
After the completion of the loop, return curr->data, which is the minimum value in the Binary search tree.
Steps for finding the maximum element in the Binary search tree:
- Declare a curr pointer pointing to the root of the Binary search tree.
- Run a while loop till curr -> right != NULL and do curr = curr -> right inside the while loop.
-
After the completion of the loop, return curr->data, which is the maximum value in the Binary search tree.
Finally, print the sum and the product of minimum and maximum values of the Binary search tree.
Let’s understand the above approach with an example:
Input:
Steps for finding the minimum element:
Initially, the curr pointer points to the root of the Binary search tree.
Now, curr->left != NULL, so we move the curr pointer to the left node, curr = curr -> left.
curr->left != NULL, so we move the curr pointer to the left node, curr = curr -> left.
Similarly, curr = curr -> left.
Now, curr -> left = NULL, return the curr -> data, which is the minimum element in the binary search tree.
Steps for finding maximum element:
Initially, the curr pointer points to the root of the Binary search tree.
Now, curr->right != NULL, so we move the curr pointer to the right node, curr = curr -> right.
Similarly, curr = curr -> right.
Here curr -> right = NULL, return the curr -> data, which is the maximum element in the Binary search tree.
Finally, print the sum and the product of minimum and maximum elements of the Binary search tree.
Check out this problem - Diameter Of Binary Tree