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

auto speedUp = []() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 0;
}();


class TreeLimits{
    public:
    int max;
    int min;
    int sum;
    TreeLimits():max(-1e5), min(1e5), sum(-1e5){}
    TreeLimits(int mx, int mn, int sm) : max(mx), min(mn), sum(sm){}
};

int ans;

class Solution {
    TreeLimits rec(TreeNode* node){
        if(node->left == NULL and node->right == NULL){
            ans = max(node->val, ans);
            return TreeLimits(node->val, node->val, node->val);
        }
        TreeLimits left, right;

        bool is_valid_bst = true;
        int both_sum = 0;
        if(node->left){
            left = rec(node->left);
            if(node->val <= left.max)    is_valid_bst = false;
            both_sum += left.sum;
        }
        
        if(node->right){
            right = rec(node->right);
            if(node->val >= right.min)   is_valid_bst = false;
            both_sum += right.sum;
        }

        if(is_valid_bst){
            int cur_max_sum = both_sum + node->val;
            ans = max(cur_max_sum, ans);
            return TreeLimits(right.max != -1e5 ? right.max : node->val, left.min != 1e5 ? left.min : node->val, cur_max_sum);
        }
        else{
            int cur_max_sum = max(left.sum, right.sum);
            ans = max(cur_max_sum, ans);
            return TreeLimits(1e5, -1e5, cur_max_sum);
        }
    }

public:

    int maxSumBST(TreeNode* root) {
        int max_sum = 0;
        ans = 0;
        if(root){
            TreeLimits tl = rec(root);
            max_sum = max(max_sum, tl.sum);
            root->left = nullptr, root->right = nullptr;
        }
        return ans;
    }
};
74391