Welcome to the daily solving of our PROBLEM OF THE DAY with Siddharth Hazra. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search Trees but also build up problem-solving skills.
In this problem, we are given two BSTs containing N1 and N2 distinct nodes respectively, and given a value x, your task is to complete the function countPairs(), which returns the count of all pairs of (a, b), where a belongs to one BST and b belongs to another BST, such that a + b = x.
Example :
Input:
BST:
5
/ \
3 7
/ \ / \
2 4 6 8
Give the problem a try before going through the video. All the best!!!
Problem Link: https://practice.geeksforgeeks.org/problems/brothers-from-different-root/1