Binary Search Tree to Greater Sum Tree

  • Amazon Interview Questions
  • Uber Interview Questions

Problem Statement

Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Sample Test Cases

Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Problem Solution

For the recursive approach, we maintain some minor “global” state so each recursive call can access and modify the current total sum.

Essentially, we ensure that the current node exists, recurse on the right subtree, visit the current node by updating its value and the total sum, and finally recurse on the left subtree.

If we know that recursing on root->right properly updates the right subtree and that recursing on root->left properly updates the left subtree, then we are guaranteed to update all nodes with larger values before the current node and all nodes with smaller values after.

Complexity Analysis

Time complexity : O(n) covertBST() gets called on each node no more than once. the recursive calls, convertBST() does a constant amount of work.

Space Complexity: O(n) Considering the worst case, a tree with only right (or only left) subtrees. The call stack will grow until the end of the longest path is reached, which in this case includes all n nodes.

Code Implementation

using namespace std;

// A BST node
class Node
{   public:
    int data;
    Node *left, *right;

// A utility function to create a new Binary Tree Node
Node *newNode(int item)
    struct Node *temp =  new Node;
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;

// Recursive function to transform a BST to sum tree.
// This function traverses the tree in reverse inorder so
// that we have visited all greater key nodes of the currently
// visited node
void transformTreeUtil(Node *root, int *sum)
   // Base case
   if (root == NULL)  return;

   // Recur for right subtree
   transformTreeUtil(root->right, sum);

   // Update sum
   *sum = *sum + root->data;

   // Store old sum in current node
   root->data = *sum - root->data;

   // Recur for left subtree
   transformTreeUtil(root->left, sum);

// A wrapper over transformTreeUtil()
void transformTree( Node *root)
    int sum = 0; // Initialize sum
    transformTreeUtil(root, &sum);

// A utility function to print indorder traversal of a
// binary tree
void printInorder(Node *root)
    if (root == NULL) return;

    cout << root->data << " ";

// Driver Program to test above functions
int main()
{   Node *root = newNode(11);
    root->left = newNode(2);
    root->right = newNode(29);
    root->left->left = newNode(1);
    root->left->right = newNode(7);
    root->right->left = newNode(15);
    root->right->right = newNode(40);
    root->right->right->left = newNode(35);

    cout << "Inorder Traversal of given tree\n";


    cout << "\n\nInorder Traversal of transformed tree\n";

    return 0;


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]