Add all greater values to every node in a given BST?

In this problem, we need to modify each node in a Binary Search Tree (BST) by adding the sum of all greater node values to the current node's value. This transforms the BST while maintaining its structure but changing the node values.

Syntax

void addGreaterValues(struct Node* root, int* sum);

Problem Statement

Given a Binary Search Tree, we need to add to each node the sum of all node values that are greater than the current node value.

Input BST

10 5 20 1 7 15 25

Output BST

70 82 45 83 77 60 25

Algorithm: Reverse Inorder Traversal

We use reverse inorder traversal (right ? root ? left) to visit nodes in descending order and maintain a running sum −

Example

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

struct Node* insert(struct Node* root, int data) {
    if (root == NULL) {
        return createNode(data);
    }
    if (data < root->data) {
        root->left = insert(root->left, data);
    } else {
        root->right = insert(root->right, data);
    }
    return root;
}

void reverseInorderAdd(struct Node* root, int* sum) {
    if (root == NULL) {
        return;
    }
    
    /* First traverse right subtree */
    reverseInorderAdd(root->right, sum);
    
    /* Add current node's value to sum and update node */
    *sum += root->data;
    root->data = *sum;
    
    /* Then traverse left subtree */
    reverseInorderAdd(root->left, sum);
}

void addGreaterValues(struct Node* root) {
    int sum = 0;
    reverseInorderAdd(root, &sum);
}

void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node* root = NULL;
    
    /* Create BST: 10, 5, 20, 1, 7, 15, 25 */
    root = insert(root, 10);
    insert(root, 5);
    insert(root, 20);
    insert(root, 1);
    insert(root, 7);
    insert(root, 15);
    insert(root, 25);
    
    printf("Original BST (Inorder): ");
    inorder(root);
    printf("
"); addGreaterValues(root); printf("Modified BST (Inorder): "); inorder(root); printf("
"); return 0; }
Original BST (Inorder): 1 5 7 10 15 20 25 
Modified BST (Inorder): 83 82 77 70 60 45 25 

How It Works

The algorithm uses reverse inorder traversal to visit nodes in descending order:

  • Step 1: Visit rightmost node first (largest value)
  • Step 2: Add current node's value to running sum
  • Step 3: Replace node's value with the accumulated sum
  • Step 4: Continue with left subtree

Time and Space Complexity

Aspect Complexity
Time Complexity O(n)
Space Complexity O(h) where h is height

Conclusion

This solution efficiently transforms a BST by adding greater values to each node using reverse inorder traversal. The approach visits each node once and maintains the BST structure while updating values in O(n) time.

Updated on: 2026-03-15T11:37:37+05:30

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements