Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 27 Jul, 2021

Hard

```
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 :

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

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:

- Both left and right subtrees are BSTs
**maxRight**of the left child is less than**Node.data****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 :

- Initialize
**ans**equal to 0. - Initialize a hash-map to store
**{ isBST, maxSum, minLeft, maxRight }**for each - 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. - The initial call is made with the given
**root**node. - 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**. - For a non-leaf node, initialise
**isBST**equal to 1 and**maxSum**,**minLeft**and**maxRight**equal to**curNode.data**. - 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. - 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. - 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. - Finally, return the value of
**ans**.

Similar problems

Kth Largest Element in BST

Moderate

Posted: 12 Mar, 2022

Two Sum IV - Input is a BST

Moderate

Posted: 22 Mar, 2022

Icarus and BSTCOUNT

Hard

Posted: 1 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Locked Binary Tree

Easy

Posted: 12 May, 2022