Given a Binary Search Tree(BST), convert it to a Binary Tree such that every key of the original BST is changed to a key plus the sum of all smaller keys in BST.
Examples:
Input:
Output:
Explanation: Every key of the original BST is changed to a key plus the sum of all smaller keys in BST.
Approach:
The idea is to use recursion to traverse the tree in inorder manner while keeping a running sum of all previously visited nodes. The value of each node is updated to this running sum, which ensure that each node contains the sum of all nodes less than or equal to it.
Below is the implementation of the above approach:
C++
// C++ program to transform a BST to// lesser sum tree#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Recursive function to transform the BST// to hold the sum of all smaller or equal keysvoidtransformToSmallerSumTree(Node*root,int&sum){if(root==nullptr){return;}// Traverse the left subtree first (smaller values)transformToSmallerSumTree(root->left,sum);// Update the sum and the current node's value// Running sum of all smaller keyssum+=root->data;// Update the node's value to the sumroot->data=sum;// Traverse the right subtree (larger values)transformToSmallerSumTree(root->right,sum);}// Function to initiate the transformationvoidtransformTree(Node*root){// Variable to store cumulative sumintsum=0;transformToSmallerSumTree(root,sum);}voidinorder(Node*root){if(root==nullptr){return;}inorder(root->left);cout<<root->data<<" ";inorder(root->right);}intmain(){// Constructing the BST// 11// / \ // 2 29// / \ / \ // 1 7 15 40// \ // 50Node*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->right=newNode(50);transformTree(root);inorder(root);cout<<endl;return0;}
C
// C program to transform a BST to // lesser sum tree#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Recursive function to transform the BST// to hold the sum of all smaller or equal keysvoidtransformToSmallerSumTree(structNode*root,int*sum){if(root==NULL){return;}// Traverse the left subtree first (smaller values)transformToSmallerSumTree(root->left,sum);// Update the sum and the current node's value// Running sum of all smaller keys*sum+=root->data;// Update the node's value to the sumroot->data=*sum;// Traverse the right subtree (larger values)transformToSmallerSumTree(root->right,sum);}// Function to initiate the transformationvoidtransformTree(structNode*root){// Variable to store cumulative sumintsum=0;transformToSmallerSumTree(root,&sum);}// Inorder traversal to print the treevoidinorder(structNode*root){if(root==NULL){return;}inorder(root->left);printf("%d ",root->data);inorder(root->right);}structNode*createNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=node->right=NULL;returnnode;}intmain(){// Constructing the BST// 11// / \ // 2 29// / \ / \ // 1 7 15 40// \ // 50structNode*root=createNode(11);root->left=createNode(2);root->right=createNode(29);root->left->left=createNode(1);root->left->right=createNode(7);root->right->left=createNode(15);root->right->right=createNode(40);root->right->right->right=createNode(50);transformTree(root);inorder(root);printf("\n");return0;}
Java
// Java program to transform a BST// to lesser sum treeimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Recursive function to transform the BST// to hold the sum of all smaller or equal keysstaticvoidtransformToSmallerSumTree(Noderoot,int[]sum){if(root==null){return;}// Traverse the left subtree first (smaller values)transformToSmallerSumTree(root.left,sum);// Update the sum and the current node's value// Running sum of all smaller keyssum[0]+=root.data;// Update the node's value to the sumroot.data=sum[0];// Traverse the right subtree (larger values)transformToSmallerSumTree(root.right,sum);}// Function to initiate the transformationstaticvoidtransformTree(Noderoot){// Variable to store cumulative sumint[]sum={0};transformToSmallerSumTree(root,sum);}staticvoidinorder(Noderoot){if(root==null){return;}inorder(root.left);System.out.print(root.data+" ");inorder(root.right);}publicstaticvoidmain(String[]args){// Constructing the BST// 11// / \// 2 29// / \ / \// 1 7 15 40// \// 50Noderoot=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.right=newNode(50);transformTree(root);inorder(root);System.out.println();}}
Python
# Python program to transform a BST # to lesser sum treeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Recursive function to transform the BST# to hold the sum of all smaller or equal keysdeftransformToSmallerSumTree(root,sum):ifrootisNone:return# Traverse the left subtree first (smaller values)transformToSmallerSumTree(root.left,sum)# Update the sum and the current node's value# Running sum of all smaller keyssum[0]+=root.data# Update the node's value to the sumroot.data=sum[0]# Traverse the right subtree (larger values)transformToSmallerSumTree(root.right,sum)# Function to initiate the transformationdeftransformTree(root):# Variable to store cumulative sumsum=[0]transformToSmallerSumTree(root,sum)# Inorder traversal to print the treedefinorder(root):ifrootisNone:returninorder(root.left)print(root.data,end=" ")inorder(root.right)if__name__=="__main__":# Constructing the BST# 11# / \# 2 29# / \ / \# 1 7 15 40# \# 50root=Node(11)root.left=Node(2)root.right=Node(29)root.left.left=Node(1)root.left.right=Node(7)root.right.left=Node(15)root.right.right=Node(40)root.right.right.right=Node(50)transformTree(root)inorder(root)print()
C#
// C# program to transform a BST // to lesser sum treeusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Recursive function to transform the BST// to hold the sum of all smaller or equal keysstaticvoidtransformToSmallerSumTree(Noderoot,refintsum){if(root==null){return;}// Traverse the left subtree first (smaller values)transformToSmallerSumTree(root.left,refsum);// Update the sum and the current node's value// Running sum of all smaller keyssum+=root.data;// Update the node's value to the sumroot.data=sum;// Traverse the right subtree (larger values)transformToSmallerSumTree(root.right,refsum);}// Function to initiate the transformationstaticvoidtransformTree(Noderoot){// Variable to store cumulative sumintsum=0;transformToSmallerSumTree(root,refsum);}staticvoidinorder(Noderoot){if(root==null){return;}inorder(root.left);Console.Write(root.data+" ");inorder(root.right);}staticvoidMain(string[]args){// Constructing the BST// 11// / \// 2 29// / \ / \// 1 7 15 40// \// 50Noderoot=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.right=newNode(50);transformTree(root);inorder(root);Console.WriteLine();}}
JavaScript
// JavaScript program to transform // a BST to lesser sum treeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Recursive function to transform the BST// to hold the sum of all smaller or equal keysfunctiontransformToSmallerSumTree(root,sum){if(root===null){return;}// Traverse the left subtree first (smaller values)transformToSmallerSumTree(root.left,sum);// Update the sum and the current node's value// Running sum of all smaller keyssum.value+=root.data;// Update the node's value to the sumroot.data=sum.value;// Traverse the right subtree (larger values)transformToSmallerSumTree(root.right,sum);}// Function to initiate the transformationfunctiontransformTree(root){// Variable to store cumulative sumletsum={value:0};transformToSmallerSumTree(root,sum);}// Inorder traversal to print the treefunctioninorder(root){if(root===null){return;}inorder(root.left);console.log(root.data+" ");inorder(root.right);}// Constructing the BST// 11// / \// 2 29// / \ / \// 1 7 15 40// \// 50letroot=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.right=newNode(50);transformTree(root);inorder(root);
Output
1 3 10 21 36 65 105 155
Time Complexity: O(n), where n is the number of nodes in given Binary Tree, as it does a simple traversal of the tree. Auxiliary Space: O(h), where h is the height of tree.