Given a binary tree root
, return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST).
Assume a BST is defined as follows:
For example:
If the root is [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
, the output should be 20 because the maximum sum in a valid Binary search tree is obtained in root node with key equal to 3 (3+2+4+5+6).
If the root is [4,3,null,1,2]
, the output should be 2 because the maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2.
If the root is [-4,-2,-5]
, the output should be 0 because all values are negatives. Return an empty BST.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxSumBST(self, root: TreeNode) -> int:
max_sum = 0
def dfs(node):
nonlocal max_sum
if not node:
return True, float('inf'), float('-inf'), 0
left_bst, left_min, left_max, left_sum = dfs(node.left)
right_bst, right_min, right_max, right_sum = dfs(node.right)
if left_bst and right_bst and left_max < node.val < right_min:
current_sum = left_sum + right_sum + node.val
max_sum = max(max_sum, current_sum)
return True, min(left_min, node.val), max(right_max, node.val), current_sum
else:
return False, float('-inf'), float('inf'), 0
dfs(root)
return max_sum
The problem requires us to find the maximum sum of keys in any subtree that is also a Binary Search Tree (BST). A BST has the property that all nodes in the left subtree are less than the root, and all nodes in the right subtree are greater than the root.
Algorithm:
Code Implementation Details:
TreeNode
class defines the structure of a node in the binary tree.Solution
class contains the maxSumBST
method, which takes the root of the binary tree as input and returns the maximum BST sum.dfs
function recursively traverses the tree and returns a tuple containing:
is_bst
: A boolean indicating whether the subtree is a BST.min_val
: The minimum value in the subtree.max_val
: The maximum value in the subtree.subtree_sum
: The sum of the nodes in the subtree.None
(empty), it's a valid BST with a sum of 0, minimum value of infinity, and maximum value of negative infinity.dfs
on the left and right subtrees.left_bst
and right_bst
).left_max < node.val
).node.val < right_min
).current_sum
) and update max_sum
if needed.Example:
For the input root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
, the algorithm identifies the subtree rooted at node 3 as the BST with the maximum sum (3 + 2 + 5 + 4 + 6 = 20).
dfs
function visits each node once.dfs
function. In the worst case (skewed tree), H can be equal to N, resulting in O(N) space complexity. In the average case (balanced tree), H is log(N), resulting in O(log N) space complexity.None
, the function should return 0, as there are no subtrees.left_max < node.val < right_min
) ensures that nodes with duplicate values in subtrees will not be considered part of a valid BST.These edge cases are correctly handled by the provided solution.