Reported September 2026
Amazonbinary tree

Maximum Sum BST in a Binary Tree

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt at this Amazon problem is checking BST validity by only comparing a node to its immediate children. It's the Maximum Sum BST in a Binary Tree question, reported in September 2026, and it looks friendlier than it is. You've got an OA coming and a tree problem on the list. The pattern is a post-order tree traversal that carries info up from each subtree. If you blank on how to pass that info, StealthCoder is the safety net running invisibly during the live assessment. Know the trick first, though.

The problem

Given the root of a binary tree, find the maximum sum of node values among all subtrees that are valid binary search trees.
A valid BST has every left-subtree value strictly smaller than its root and every right-subtree value strictly larger. Return 0 when every valid non-empty BST subtree has a negative sum.

Function
maxSumBST(root: TreeNode) → int

Examples
Example 1
root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
return = 20
The subtree rooted at 3 is a BST with sum 20.
Example 2
root = [4,3,null,1,2]
return = 2
The leaf with value 2 is the best valid BST subtree.
Example 3
root = [-4,-2,-5]
return = 0
All valid BST sums are negative, so zero is returned.

Constraints
The tree contains between 0 and 5000 nodes.
-10000 ≤ Node.val ≤ 10000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is post-order DFS. For each node, return a tuple from its children: is it a BST, min value, max value, and sum. A node is a valid BST if the left child is a BST, the right child is a BST, left max < node value, and right min > node value. Then the sum is left sum + right sum + node value, and you update a global best. The pitfall is comparing only to direct children, which accepts trees where a deeper node breaks the rule. Another one is forgetting empty subtrees: return min as infinity, max as negative infinity, sum 0, so they pass cleanly. Init the answer to 0 so all-negative trees return 0. That's O(n) time. If you freeze on the tuple design during the live OA, StealthCoder can hand you the structure as a hedge.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximum Sum BST in a Binary Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum sum bst in binary tree. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Sum BST in a Binary Tree FAQ

What's the trick for Maximum Sum BST in a Binary Tree?+

Do a post-order traversal and return four things per node: whether it's a BST, the subtree min, the subtree max, and the sum. Validate a node using the left max and right min, not just the immediate children. Update a global answer whenever a valid BST is found.

Why does checking only direct children fail?+

A BST needs every value in the left subtree below the root and every value in the right subtree above it. A grandchild can violate that while the direct children look fine. Passing min and max up from each subtree catches those cases.

How do I handle empty subtrees and negative sums?+

Treat a null node as a valid BST with min set to infinity, max set to negative infinity, and sum 0. That lets the comparisons work without special cases. Start the global best at 0, so all-negative trees return 0 as the problem requires.

How hard is this really for an Amazon OA?+

It's a hard-tier tree problem, but the solution is short once you see the post-order tuple idea. The difficulty is design, not code. If you've written a validate-BST with bounds before, you're most of the way there.

How do I prepare in 48 hours?+

Write the post-order tuple solution from scratch twice, then trace Example 1 by hand to confirm the sum of 20. Test the edge cases: empty tree, single node, and all-negative values. Aim for O(n) time with recursion depth in mind for 5000 nodes.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it