Skip to content

Practice Maximum Sum BST in Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1373
LeetCode Problem

Maximum Sum BST in Binary Tree

Problem Statement

Return the greatest sum of node values in a subtree of root that is a binary search tree. For every node in that subtree, all values in its left subtree must be strictly smaller and all values in its right subtree strictly larger, with both subtrees satisfying the same rule. An empty BST is allowed and has sum 0.

Source: Maximum Sum BST in Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
Output
20

# Example 2

Input
root = [4,3,null,1,2]
Output
2

# Example 3

Input
root = [-4,-2,-5]
Output
0

Constraints

The number of nodes in the tree is in the range [1, 4 * 10^4].
-4 * 10^4 <= Node.val <= 4 * 10^4

Related LeetCode Problems

Practice more problems similar to Maximum Sum BST in Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.