         } else {
             rightSum = 0
         }
+        
+        /// If current tree rooted from `root` is a valid BST.
         if isValid {
-            maxSum = max(maxSum, sum + leftSum + rightSum)
+            sum += leftSum + rightSum
+            maxSum = max(maxSum, sum)
+        } else {
+            sum = 0
         }
-        return (isValid, minV, maxV, isValid ? sum + leftSum + rightSum : 0)
+        
+        return (isValid, minV, maxV, sum)
     }
 }

        } else {
            leftSum = 0
        }
        if let right = root.right {
            let (rvalid, rmin, rmax, rsum) = self.validBST(right)
            if rmin <= maxV { isValid = false }

 *     }
 * }
 */

 /**
  *
  *  
  */
class Solution {
    func maxSumBST(_ root: TreeNode?) -> Int {
        // Init.
        self.maxSum = 0
        
        guard let root = root else { return self.maxSum }
