Replace Each BST Value with Its Original Greater-or-Equal Sum

Quick Overview

Replace Each BST Value with Its Original Greater-or-Equal Sum. Convert the given binary search tree so each node's new value is its original value plus every original tree value strictly greater than it.

Replace Each BST Value with Its Original Greater-or-Equal Sum

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Convert the given binary search tree so each node's new value is its original value plus every original tree value strictly greater than it. Return the root, keeping the tree's links and nodes. The original BST has unique values, with every left descendant smaller and every right descendant larger than its ancestor. ```cpp class Solution { public: TreeNode* bstToGst(TreeNode* root); }; ``` ### Examples **Example 1:** ```text Input: root = [2,1,3] Output: [5,6,3] Explanation: Original2 becomes2+3=5;1 becomes1+2+3=6;3 remains3. ``` **Example 2:** ```text Input: root = [1,null,4] Output: [5,null,4] Explanation: The root adds the larger original value4, and the right node stays4. ``` ### Constraints - The tree contains1 to100 nodes. - `0 <= Node.val <= 100`. - All original values are unique. - The original tree satisfies strict BST ordering. ### Contract considerations Keep the original `TreeNode*` input and result. Update the existing nodes in place and return the same root pointer. ```hint Read the boundary condition Which tree traversal visits larger original keys before smaller ones? ``` ### Boundary reminders Keep the original `TreeNode*` input and result. Update the existing nodes in place and return the same root pointer.

Overview: Replace Each BST Value with Its Original Greater-or-Equal Sum. Convert the given binary search tree so each node's new value is its original value plus every original tree value strictly greater than it.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Convert the given binary search tree so each node's new value is its original value plus every original tree value strictly greater than it. Return the root, keeping the tree's links and nodes. The original BST has unique values, with every left descendant smaller and every right descendant larger than its ancestor.

class Solution {
public:
    TreeNode* bstToGst(TreeNode* root);
};

Examples

Example 1:

Input: root = [2,1,3]
Output: [5,6,3]
Explanation: Original2 becomes2+3=5;1 becomes1+2+3=6;3 remains3.

Example 2:

Input: root = [1,null,4]
Output: [5,null,4]
Explanation: The root adds the larger original value4, and the right node stays4.

Constraints

  • The tree contains1 to100 nodes.
  • 0 <= Node.val <= 100 .
  • All original values are unique.
  • The original tree satisfies strict BST ordering.

Contract considerations

Keep the original TreeNode* input and result. Update the existing nodes in place and return the same root pointer.

Boundary reminders

Keep the original TreeNode* input and result. Update the existing nodes in place and return the same root pointer.