Maximum Sum BST in Binary Tree - Complete Solution Guide
Maximum Sum BST in Binary Tree is LeetCode problem 1373, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: Input: root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6] Output: 20 Explanation: Maximum
Detailed Explanation
The problem asks us to find the maximum sum of keys within any subtree of a given binary tree, but with the added constraint that this subtree must also be a Binary Search Tree (BST). A BST has the property that for each node, all keys in its left subtree are smaller than the node's key, and all keys in its right subtree are larger. We need to traverse the tree, identify all valid BST subtrees, calculate their sums, and return the maximum sum encountered.
Solution Approach
The solution uses a post-order Depth-First Search (DFS) traversal of the binary tree. For each node, it recursively checks if its left and right subtrees are BSTs. If both are BSTs and the node's value is within the valid range (greater than the maximum value of the left subtree and smaller than the minimum value of the right subtree), then the current subtree is also a BST. The sum of this BST is calculated, and the global maximum sum is updated. If any subtree is not a BST, the node is marked as not being part of any BST, and the search continues.
Step-by-Step Algorithm
- Step 1: Define a recursive helper function `traverse(node)` that performs a post-order traversal.
- Step 2: Base Case: If the current `node` is null, return a `Result` object indicating it's a BST (vacuously true), with minimum value as infinity, maximum value as negative infinity, and sum as 0.
- Step 3: Recursively call `traverse` on the left and right children of the current `node` to get their respective `Result` objects (which contain BST status, min/max values, and subtree sum).
- Step 4: Check if the current subtree, including the current `node`, forms a valid BST. This is true if: a) both left and right subtrees are BSTs, b) the current node's value is greater than the maximum value in the left subtree, and c) the current node's value is less than the minimum value in the right subtree.
- Step 5: If the current subtree is a BST, calculate its sum by adding the current node's value to the sums of its left and right subtrees. Update the global maximum sum if the current subtree's sum is larger. Also, determine the new minimum and maximum values for the current subtree (the minimum of the left subtree's minimum and the node's value, and the maximum of the right subtree's maximum and the node's value, respectively). Return a Result object indicating that it is a BST, the min, max, and sum.
- Step 6: If the current subtree is NOT a BST (any of the conditions in Step 4 are false), then return a `Result` object indicating that it's not a BST, setting min as negative infinity, max as positive infinity, and sum to 0. This prevents the parent node from considering it a BST.
- Step 7: The `maxSumBST` function initializes the `max_sum` to 0 and calls the `traverse` function on the root. Finally, it returns the `max_sum`.
Key Insights
- Insight 1: Post-order traversal allows us to determine if a subtree is a BST and calculate its sum bottom-up.
- Insight 2: We need to track not only if a subtree is a BST, but also the minimum and maximum values within that subtree for efficient validation at each node.
- Insight 3: The solution must handle cases where the entire tree is not a BST, or contains only negative values, potentially resulting in a maximum BST sum of 0.
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(H)
Topics
This problem involves: Dynamic Programming, Tree, Depth-First Search, Binary Search Tree, Binary Tree.
Companies
Asked at: Zepto.