Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Binary Search Tree to Greater Sum Tree

XPChallenge Points: 20
levelLevel: Medium

You are given the root node of a Binary Search Tree (BST). Your task is to convert the BST into a Greater Sum Tree such that every node’s value becomes the original node value plus the sum of all node values that are greater than it in the tree.

A Binary Search Tree follows these rules:

  • Values in the left subtree are strictly smaller than the node’s value.

  • Values in the right subtree are strictly larger than the node’s value.

  • Both left and right subtrees must also satisfy the BST property.

Your goal is to adjust the node values accordingly and return the modified root.

Example 1:

Input: root = [5,3,8,2,4,7,9]

Output: [24,31,17,33,30,24,9]

Explanation:

Each node value is replaced with itself + sum of values greater than it.

Example 2:

Input: root = [2,1,3]

Output: [5,6,3]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result