Last Updated: 27 Jul, 2021

Maximum Sum BST

Hard
Asked in companies
AmazonOLX GroupExpedia Group

Problem statement

You are given a Binary Tree ‘root’. The given Binary Tree may or may not be a Binary Search Tree(BST) itself. Your task is to find the maximum sum of node values of any subtree that is a Binary Search Tree(BST).

Binary Search Tree is defined as follows:
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.
Example :

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.

 

Input Format :
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 :

Example

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).
Output Format :
For each test case print the maximum sum possible.

Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10      
1 <= Number Of Nodes <= 5*10^4
0 <= Node.data <= 10^5

Time limit: 1 sec
Sample Input 1 :
2
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 -1 -1 -1 -1
Sample Output 1 :
28
3
Explanation Of Sample Output 1 :

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.
Sample Input 2 :
2
2 1 3 -1 -1 -1 -1
1 3 2 2 4 1 3 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 2 :
6
9

Approaches

01 Approach

 

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 :

  1. Initialize ans equal to 0.
  2. Create a recursive function traverseTree which takes the following parameters: curNode to point the current node and ans to store the maximum possible sum.
  3. The initial call is made with the given root node.
  4. In traverseTree for each node check if subtree rooted at current node is a BST.
  5. If found a BST, calculate sum of the subtree rooted at the current node.
  6. Check if sum is greater than current value of ans, if so set ans equal to sum.
  7. Check if the left/right child exists, and recursively calculate same things for them.
  8. Return the value of ans.

02 Approach

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:

  1. Both left and right subtrees are BSTs
  2. maxRight of the left child is less than Node.data
  3. minLeft of the right child is greater than Node.data

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 :

  1. Initialize ans equal to 0.
  2. Initialize a hash-map to store { isBST, maxSum, minLeft, maxRight } for each node.
  3. Create a recursive function traverseTree which takes the following parameters: curNode to point the current node, mp the hash-map to updated, ans to store the maximum possible sum.
  4. The initial call is made with the given root node.
  5. In traverseTree check if the curNode is a leaf node, if it is found to be a leaf node then update isBST equal to 1 and maxSum, minLeft and maxRight equal to curNode.data.
  6. For a non-leaf node, initialise isBST equal to 1 and maxSum, minLeft and maxRight equal to curNode.data.
  7. Now for a non-leaf node, if its left child exists make a recursive call to traverseTree. Once the function call is returned, if the left subtree was not a BST or the maximum value of the left subtree is greater than equal to Node.data then set isBST equal to 0 for the current node. Increment maxSum for the current node by maxSum of the left subtree. Also, set minLeft equal to minLeft of the left subtree.
  8. Similarly, if the right child exists, make a recursive call to traverseTree. Once the function call is returned, if the right subtree was not a BST or the minimum value of the right subtree is less than equal to Node.data then set isBST equal to 0 for the current node. Increment maxSum for the current node by maxSum of the right subtree. Also, set maxRight equal to maxRight of the right subtree.
  9. If the subtree rooted at the current node is a BST then isBST is equal to 1, for this case update the ans with maxSum if needed.
  10. Finally, return the value of ans.