The problem. Given the root of a binary tree (not necessarily a BST), return the largest sum of all values in any subtree that is a binary search tree. The empty tree counts as a BST with sum 0, so the answer is never negative.
root = [1, 4, 3, 2, 4, 2, 5, null, null, null, null, null, null, 4, 6] -> 20
root = [4, 3, null, 1, 2] -> 2 (just the leaf 2)
root = [-4, -2, -5] -> 0 (the empty BST)
root = [2, 1, 3] -> 6Members only · free account
The rest of this lesson, with 5 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Was this helpful?
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson