<p>Given a <strong>binary tree</strong> <code>root</code>, return <em>the maximum sum of all keys of <strong>any</strong> sub-tree which is also a Binary Search Tree (BST)</em>.</p>

<p>Assume a BST is defined as follows:</p>

<ul>
	<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
	<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
	<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/01/30/sample_1_1709.png" style="width: 320px; height: 250px;" /></p>

<pre>
<strong>Input:</strong> root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
<strong>Output:</strong> 20
<strong>Explanation:</strong> Maximum sum in a valid Binary search tree is obtained in root node with key equal to 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/01/30/sample_2_1709.png" style="width: 134px; height: 180px;" /></p>

<pre>
<strong>Input:</strong> root = [4,3,null,1,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = [-4,-2,-5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All values are negatives. Return an empty BST.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 4 * 10<sup>4</sup>]</code>.</li>
	<li><code>-4 * 10<sup>4</sup> &lt;= Node.val &lt;= 4 * 10<sup>4</sup></code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */


 struct Node {
    Node* left;
    Node* right;

    int minVal;
    int maxVal;
    bool isBST;
    int sum;
 };
 
class Solution {
public:

    Node* maxSumBSTImpl( TreeNode* root, int& globalMax )
    {
        Node* node = new Node();
        node->minVal = root->val;
        node->maxVal = root->val;
        node->isBST = true;
        node->sum = 0;

        if( root->left != NULL )
        {
            node->left = maxSumBSTImpl( root->left, globalMax );

            node->minVal = min( node->minVal, node->left->minVal );
            node->maxVal = max( node->maxVal, node->left->maxVal );

            if( root->val <= node->left->maxVal || !node->left->isBST )
                node->isBST = false;
        }

        if( root->right != NULL )
        {
            node->right = maxSumBSTImpl( root->right, globalMax );

            node->minVal = min( node->minVal, node->right->minVal );
            node->maxVal = max( node->maxVal, node->right->maxVal );

            if( root->val >= node->right->minVal || !node->right->isBST )
                node->isBST = false;
        }

        int leftSum = (node->left!=NULL) ? node->left->sum : 0;
        int rightSum = (node->right!=NULL) ? node->right->sum : 0;

        if (node->isBST)
            node->sum = root->val + leftSum + rightSum;
        else
            node->sum = max( leftSum, rightSum );

        globalMax = max( globalMax, node->sum );

        if (node->left != NULL)
        {
            Node* temp = node->left;
            node->left = NULL;
            delete temp;
        }

        if (node->right != NULL)
        {
            Node* temp = node->right;
            node->right = NULL;
            delete temp;
        }

        return node;
    }

    int maxSumBST(TreeNode* root) {
        
        if( root == NULL )
            return 0;
        
        int globalMax = INT_MIN;

        Node* node = maxSumBSTImpl(root, globalMax );
        delete node;

        return max( globalMax, 0 );
    }
};
161676