FunctionRacerProblemsRace

Convert BST to Greater Tree

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Change every value in the BST to its original value plus the sum of all original values that are larger. Keep the shape of the tree. Return the root. Values are distinct.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [4,1,6,0,2,5,7]
returns [22,25,13,25,24,18,7]

The root 4 becomes 4+5+6+7 = 22; 7 stays 7; 0 becomes 0+1+2+4+5+6+7 = 25.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems