<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) {}
 * };
 */
// class Solution {
// public:
//     // bool isBST(TreeNode* root){
//     //     if(root==NULL){
//     //         return true;
//     //     }

//     //     if(root->right && root->left){
//     //         return (root->val > root->left->val) && (root->right->val>root->val) && isBST(root->left) && isBST(root->right);
//     //     }
//     //     else if(root->right){
//     //         return (root->right->val > root->val) && isBST(root->right);
//     //     }
//     //     else if(root->left){
//     //         return (root->left->val < root->val) && isBST(root->left);
//     //     }
//     //     else{
//     //         return true;
//     //     }

//     // }

//     void inorder(TreeNode* root,vector<int>&ans){
//         if(root==NULL){
//             return;
//         }
//         inorder(root->left,ans);
//         ans.push_back(root->val);
//         inorder(root->right,ans);
//     }

//     bool isBST(TreeNode* root){
//         vector<int>ans;
//         inorder(root,ans);
//         for(int i=1;i<ans.size();i++){
//             if(ans[i]<=ans[i-1]){
//                 return false;
//             }
//         }
//         return true;
//     }

//     int Sum(TreeNode* root){
//         if(root==NULL){
//             return 0;
//         }
//         int curr_sum=root->val;
//         curr_sum+=Sum(root->left);
//         curr_sum+=Sum(root->right); 
//         return curr_sum;
//     }

//     void dfs(TreeNode* root, int &maxi){
//         if(root==NULL){
//             return;
//         }
//         if(isBST(root)){
//             maxi=max(maxi,Sum(root));
//         }
//         dfs(root->left,maxi);
//         dfs(root->right,maxi);
//     }

//     int maxSumBST(TreeNode* root) {
//         int maxi=0;
//         dfs(root,maxi);
//         return maxi;
//     }
// };

class Solution {
public:
    void dfs(TreeNode* root, int& maxi, int& curr_sum, int& min_val, int& max_val, bool& is_bst) {
        if (root == NULL) {
            is_bst = true;
            curr_sum = 0;
            min_val = INT_MAX;
            max_val = INT_MIN;
            return;
        }

        
        int left_sum = 0, right_sum = 0;
        int left_min = INT_MAX, right_min = INT_MAX;
        int left_max = INT_MIN, right_max = INT_MIN;
        bool left_is_bst = true, right_is_bst = true;

        
        dfs(root->left, maxi, left_sum, left_min, left_max, left_is_bst);
        dfs(root->right, maxi, right_sum, right_min, right_max, right_is_bst);

        
        if (left_is_bst && right_is_bst && root->val > left_max && root->val < right_min) {
            
            curr_sum = root->val + left_sum + right_sum;
            min_val = min(root->val, left_min);
            max_val = max(root->val, right_max);

           
            maxi = max(maxi, curr_sum);
            is_bst = true;
        } else {
            
            curr_sum = 0;
            min_val = 0;
            max_val = 0;
            is_bst = false;
        }
    }

    int maxSumBST(TreeNode* root) {
        int maxi = 0;
        int curr_sum = 0, min_val = 0, max_val = 0;
        bool is_bst = true;
        dfs(root, maxi, curr_sum, min_val, max_val, is_bst);
        return maxi;
    }
};


120018