


1) If the left subtree exists it should contain only nodes with values less than the current node's value.
2) If the right subtree exists it should contain only nodes with values greater than the current node's value.
3) Both the left and right subtrees should also be Binary Search Tree.

For the above binary tree, the BST with the maximum possible sum is marked with RED colour, the sum of this BST is equal to 6.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows
The first line of each test case contains the elements of the tree in the level order form separated by a single space.
If any node does not have a left or right child, take -1 in its place.
For example, the input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
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).
For each test case print the maximum sum possible.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= Number Of Nodes <= 5*10^4
0 <= Node.data <= 10^5
Time limit: 1 sec
2
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 -1 -1 -1 -1
28
3

For test case 1, the image above shows the input tree:
Given binary tree is itself a BST, also as all the nodes have positive values hence entire tree will be the subtree with maximum sum. Sum of all the node values is: 1 + 2 + 3 + 4 +5 + 6 + 7 = 28

For test case 2, the image above shows the input tree:
The subtree with a single node and node value equal to 3 is the BST with maximum sum.
2
2 1 3 -1 -1 -1 -1
1 3 2 2 4 1 3 -1 -1 -1 -1 -1 -1 -1 -1
6
9
Traverse the tree using recursion. For each node first check if it is a BST, if it is a BST then calculate the sum of node values of the subtree rooted at the current node. Use variable ans to store the maximum BST sum calculated till now, and finally return the value of ans.
The steps are as follows :
Traverse the tree using recursion. Store the following 4 values: { isBST, maxSum, minLeft, maxRight } corresponding to each node in a hash-map. If the node is a leaf node, it will be a BST with maxSum, minLeft and maxRight equal to leafNode.data.
For all other nodes, it is considered BST if the following 3 conditions are satisfied:
If the above-mentioned properties are satisfied for a node, set isBST equal to 1, its maxSum equal to Node.data + left child’s maxSum + right child’s maxSum.
Initially set minLeft and maxRight equal to Node.data, if the left child exists, then minLeft is the minimum of current minLeft and left child’s minLeft, similarly if right child exists maxRight is maximum of the current value of maxRight and right child’s maxRight. (we initially set the values of the current node’s minLeft and maxRigth equal to Node.data as there is a possibility that the left/right child may not exist, but the subtree could still be a BST).
Initialize ans equal to 0. For each node, if it is a BST, check if maxSum is greater than ans. Finally, return the value of ans.
The steps are as follows :
Two Sum IV - Input is a BST
Icarus and BSTCOUNT
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Locked Binary Tree
Maximum Island Size in a Binary Tree