Two Sum in a BST

You have been given a Binary Search Tree and a target value. You need to find out whether there exists a pair of node values in the BST, such that their sum is equal to the target value.

A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree whose internal nodes each store a value greater than all the values keys in the node's left subtree and less than those in its right subtree.

Follow Up:
Can you solve this in O(N) time, and O(H) space complexity?
Input format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow. 

The first line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place. 

The second line of each test case contains a single integer representing the target value.

For example, the input for the tree depicted in the below image would be :

bst_example

4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1

Explanation :

Level 1 :
The root node of the tree is 4

Level 2 :
Left child of 4 = 2
Right child of 4 = 6

Level 3 :
Left child of 2 = 1
Right child of 2 = 3
Left child of 6 = 5
Right child of 6 = 7

Level 4 :
Left child of 1 = null (-1)
Right child of 1 = null (-1)
Left child of 3 = null (-1)
Right child of 3 = null (-1)
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:    
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
Output format:
For each test case, print True or False in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 3000
-10^9 <= node data <= 10^9, (where node data != -1)
-10^9 <= target value <= 10^9

Where N denotes is the number of nodes in the given tree.

Time Limit: 1 second
CodingNinjas
author
2y

Tip 1 : Practise questions on tree and graph
 

CodingNinjas
author
2y
Brute-force

The idea is to traverse the BST in a level-order manner and let’s denote the value of the current node as A. For each node, check whether the node with value (target - A) is present in the ...read more

CodingNinjas
author
2y
Using Set

In the previous approach, for each node in the BST, we were searching for a second node with the value (target - value of the current node) again and again. We can optimize this search by usi...read more

CodingNinjas
author
2y
Using Two Pointers

We can use the property of the inorder traversal of the BST i.e. inorder traversal of a BST always traverses the nodes in increasing order of their values. So, we perform inorder tra...read more

CodingNinjas
author
2y
Two Pointer on BST

The idea is the same as the previous approach with space optimization, by applying the two-pointer technique on the given BST. In this approach, we will maintain a forward and a back...read more

Add answer anonymously...
Morgan Stanley Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter