<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.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class NodeInfo:
    def __init__(
        self, 
        is_bst: bool, 
        sum: int = 0, 
        best_bst_sum: int = 0,
        min: int = None,
        max: int = None,
    ):
        self.is_bst = is_bst
        self.sum = sum
        self.best_bst_sum = best_bst_sum
        self.min = min
        self.max = max
        
class Solution:
    def is_bst(self, root: TreeNode, left_info: NodeInfo, right_info: NodeInfo) -> bool:
        if not left_info.is_bst or not right_info.is_bst:
            return False

        if left_info.max is not None and left_info.max >= root.val:
            return False

        if right_info.min is not None and right_info.min <= root.val:
            return False

        return True

    def compute_min(self, root: TreeNode, left_info: NodeInfo) -> int:
        if left_info.min:
            return left_info.min
        return root.val

    def compute_max(self, root: TreeNode, right_info: NodeInfo) -> int:
        if right_info.max:
            return right_info.max
        return root.val

    def compute_best_bst_sum(
        self, 
        is_bst: bool, 
        sum: int, 
        left_info: NodeInfo, 
        right_info: NodeInfo
    ) -> int:
        best_bst_sum = max(left_info.best_bst_sum, right_info.best_bst_sum)
        if is_bst:
            best_bst_sum = max(best_bst_sum, sum)
        return best_bst_sum

    
    def maxSumBSTCheck(self, root: Optional[TreeNode]) -> NodeInfo:
        if root is None:
            return NodeInfo(True)

        left_node_info = self.maxSumBSTCheck(root.left)
        right_node_info = self.maxSumBSTCheck(root.right)

        is_bst = self.is_bst(root, left_node_info, right_node_info)
        min = self.compute_min(root, left_node_info)
        max = self.compute_max(root, right_node_info)
        sum = root.val + left_node_info.sum + right_node_info.sum
        best_bst_sum = self.compute_best_bst_sum(is_bst, sum, left_node_info, right_node_info)
        return NodeInfo(is_bst, sum, best_bst_sum, min, max)

    def maxSumBST(self, root: Optional[TreeNode]) -> int:
        return self.maxSumBSTCheck(root).best_bst_sum
334