Start learning free

LESSON 484 OF 673 · BINARY TREES

Maximum Sum BST in Binary Tree (Python, Time Complexity: O(n)):

Practice O(n2) Read the full explanation

You get the root of a binary tree. Return the largest sum of values in a subtree that is a binary search tree (BST).

A subtree is a node together with everything below it. It is a BST when this holds at each of its nodes:

  • every value in the node's left subtree is smaller than the node's value,
  • every value in its right subtree is larger.

An empty tree also counts as a BST, with a sum of 0. So the answer is never below 0.

The trees below are listed level by level, top to bottom and left to right. A missing child is written as None.

 

Example 1:

6 9 3 4 11 2 1 5

Input: root = [6,9,3,4,11,None,2,1,5]
Output: 30
Explanation: The blue subtree starts at 9.
It is a BST: 1 < 4 < 5 < 9 < 11.
Its sum is 9 + 4 + 11 + 1 + 5 = 30.
The whole tree is not a BST: 9 is left of 6 but larger.
The subtree from 3 down is not a BST: 2 is right of 3 but smaller.

Example 2:

-5 -8 4 -9 7

Input: root = [-5,-8,4,-9,7]
Output: 7
Explanation: The subtree from -8 down is a BST, but its sum is -10.
The whole tree is not a BST: 7 is left of -5 but larger.
The best BST is the single blue node 7.

Example 3:

Input: root = [-3,-6,-1]
Output: 0
Explanation: The whole tree is a BST, but every value is negative.
So any BST with nodes in it has a sum below 0.
The empty tree, with sum 0, is the best.

 

Constraints:

  • 1 to 25,000 nodes.
  • Each value is from -10,000 to 10,000.
  • The tree can be up to 10,000 levels deep.
STEP 1 OF 20
    Ctrl + Enter to check

    PYTHON · BINARY TREES

    Max Sum Bst in Python, explained

    Understanding the Problem

    The core challenge of this problem is to identify the largest sum of keys in any subtree that is a valid Binary Search Tree (BST). A BST is defined by the properties that all nodes in the left subtree are less than the root, and all nodes in the right subtree are greater than the root. Both subtrees must also be BSTs.

    This problem is significant in various applications such as database indexing, where BST properties are used to maintain sorted data for efficient retrieval.

    Potential pitfalls include incorrectly identifying BSTs or not considering all possible subtrees.

    Approach

    To solve this problem, we need to traverse the tree and check each subtree to see if it is a valid BST. A naive approach would involve checking each subtree individually, which would be inefficient. Instead, we can use a post-order traversal to gather information about each subtree and determine if it forms a valid BST.

    We will use a helper function that returns multiple values for each node: whether the subtree is a BST, the sum of the subtree, the minimum value in the subtree, and the maximum value in the subtree. This information will help us efficiently determine the maximum sum of any BST subtree.

    Algorithm

    1. Define a helper function that performs a post-order traversal.

    2. For each node, recursively check its left and right subtrees.

    3. Gather information about whether the left and right subtrees are BSTs, their sums, and their min/max values.

    4. Determine if the current subtree is a BST based on the gathered information.

    5. If it is a BST, update the maximum sum if the current subtree's sum is greater.

    6. Return the necessary information to the parent node.

    Code Implementation

    class TreeNode:
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    def maxSumBST(root):
        def postorder(node):
            if not node:
                return (True, 0, float('inf'), float('-inf'))
            
            left_is_bst, left_sum, left_min, left_max = postorder(node.left)
            right_is_bst, right_sum, right_min, right_max = postorder(node.right)
            
            if left_is_bst and right_is_bst and left_max < node.val < right_min:
                current_sum = left_sum + node.val + right_sum
                max_sum[0] = max(max_sum[0], current_sum)
                return (True, current_sum, min(left_min, node.val), max(right_max, node.val))
            else:
                return (False, 0, 0, 0)
        
        max_sum = [0]
        postorder(root)
        return max_sum[0]
    
    # Example usage:
    # root = TreeNode(1, TreeNode(4, TreeNode(2), TreeNode(4)), TreeNode(3, TreeNode(2), TreeNode(5, TreeNode(4), TreeNode(6))))
    # print(maxSumBST(root))  # Output: 20
    

    Complexity Analysis

    The time complexity of this approach is O(n), where n is the number of nodes in the tree. This is because we visit each node exactly once during the post-order traversal. The space complexity is O(h), where h is the height of the tree, due to the recursion stack.

    Edge Cases

    1. An empty tree should return 0.

    2. A tree with all negative values should return 0, as the best BST would be an empty one.

    3. Trees with only one node should return the value of that node.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple trees with a few nodes.
    • Large trees with mixed positive and negative values.
    • Edge cases such as empty trees and trees with all negative values.

    Using a testing framework like unittest or pytest can help automate and validate these test cases.

    Thinking and Problem-Solving Tips

    1. Break down the problem into smaller subproblems.

    2. Use helper functions to manage complex logic.

    3. Think about the properties of the data structure (BST) and how they can be leveraged.

    4. Practice similar problems to improve problem-solving skills.

    Conclusion

    Understanding and solving the "Maximum Sum BST in Binary Tree" problem involves leveraging the properties of BSTs and using efficient traversal techniques. By breaking down the problem and using a post-order traversal, we can efficiently determine the maximum sum of any BST subtree. Practicing such problems helps improve problem-solving skills and understanding of tree data structures.

    Additional Resources

    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free