


• The left subtree of a node contains only nodes with data less than the node’s data.
• The right subtree of a node contains only nodes with data greater than the node’s data.
• Both the left and right subtrees must also be binary search trees.
1. All the elements of the Binary Search Tree are unique.
2. You can’t use the same node value/element of BST twice.
tree: 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
'K' = 13,

The nodes with values 8 and 5 as shown in the above figure gives sum equal to the given target 13.
Therefore, the output will be “true” i.e it is possible to find a pair in the given BST having sum equal to ‘K’.
The first line contains elements of the Binary Search Tree in the level order form. The input consists of values of nodes separated by a single space in a single line. In case, a node is null, we take -1 in its place.
The second line contains a single integer ‘K’ which denotes the target value.
The input for the tree depicted in the below image would be :

20 10 35 5 15 30 42 -1 -1 13 -1 -1 -1 -1 -1 -1 -1
The only line contains "true" if there exist two nodes in the given BST such that the sum of their values is equal to the given target ‘K’, otherwise "false".
You don’t need to print the output, it has already been taken care of. Just implement the given function.
The idea is that if the sum of any two elements say, ‘A’ and ‘B’ is equal to the given value ‘K’, and we already know that ‘A’ is present in the given tree then we only need to check whether element ‘B’ = ‘K’ - ‘A’ exists in the tree or not. We can check this by using the property of the binary search tree.
Approach :
In this approach, we use the property of the inorder traversal of BST. The inorder traversal of a BST always traverses the nodes of BST in the increasing order of their values.
Thus, we do the inorder traversal of the given tree and we store all the values of the nodes in an array. This will give us an array in increasing order. Now we can use the two-pointer technique on this sorted list for determining if two elements exist in the list which sums up to the target value ‘K’.
Approach :
Description of ‘inorder’ function
This function will take two parameters :
void inorder(ROOT, NUMS):
Instead of searching a second value for every node of the given BST, we can optimise our search by using a set to keep track of elements.
In this approach, we will maintain a ‘FRONT’ and a ‘BACK’ iterator pointers that will iterate the BST in the order of in-order and reverse in-order traversal respectively.
Approach :
Guess Price
Unique BSTs
Unique BSTs
Unique BSTs
Kth Largest Element in BST
Two Sum IV - Input is a BST
Icarus and BSTCOUNT