Given the root of a binary tree, find the maximum sum of all keys of any subtree that is also a valid Binary Search Tree (BST).
A valid Binary Search Tree satisfies the following conditions:
The left subtree of a node contains only nodes with keys strictly less than the node’s key.
The right subtree of a node contains only nodes with keys strictly greater than the node’s key.
Both the left and right subtrees must also be valid Binary Search Trees.
Note: An empty subtree is considered a valid Binary Search Tree with a sum of
.
Constraints:
The number of nodes in the tree is in the range
Node.val
Given the root of a binary tree, find the maximum sum of all keys of any subtree that is also a valid Binary Search Tree (BST).
A valid Binary Search Tree satisfies the following conditions:
The left subtree of a node contains only nodes with keys strictly less than the node’s key.
The right subtree of a node contains only nodes with keys strictly greater than the node’s key.
Both the left and right subtrees must also be valid Binary Search Trees.
Note: An empty subtree is considered a valid Binary Search Tree with a sum of
.
Constraints:
The number of nodes in the tree is in the range
Node.val