


Can you solve this in O(N) time, and O(H) space complexity?
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 :
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
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).
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
For each test case, print True or False in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
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
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 BST or not. We can search this node in the given tree using the BST property, i.e. at any node we go to left subtree or right subtree by comparing the node value to (target - A), and if the value is found in the BST, return true. If the value is not found for any node, return false.
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 using a HashSet to keep track of the elements of the BST. So we will traverse the tree in a depth-first manner, and for any current node of value A, we check if there is a value present in the HashSet which is equal i.e. target - A. If so, we can say that there are two nodes with the values such that their sum is equal to target, Otherwise, we put the value to the HashSet.
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 traversal on the BST and store the values in an auxiliary array. As this array is sorted, so now we can use the two-pointer technique on this sorted array for determining if two elements exist in the array which sums up to the target value.
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 backward iterator that will iterate the BST in the order of in-order and reverse in-order traversal respectively using the two stacks, one maintaining the leftmost value of the BST, start, and one maintaining the rightmost value of BST, end. Now, using the two-pointer technique, we check if the sum of values at the start and end nodes are equal to the target. If they are equal to the target value, then return true. If the values are lesser than the target value, we make forward iterator point to the next element using stack, else if the values are greater than the target value, we make backward iterator point to the previous element using the second stack.
At last, if we find no such two elements, then return false.
Guess Price
Unique BSTs
Unique BSTs
Unique BSTs
Kth Largest Element in BST
Two Sum IV - Input is a BST
Icarus and BSTCOUNT