Add all greater values to every node in a BST | Practice | GeeksforGeeks

Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
           50
         /    \
        30    70
      /   \   / \  
     20   40 60  80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
             260
          /       \
        330      150
       /   \   /     \
    350   300 210    80

Example 2:
Input:
          2
        /   \
       1     5
            /  \
           4    7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000

Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
0
pushkarcoding101 month ago
python solution (0.2/1.0)
 
def modify(root):   ar=inor(root,[])   ar=ar[::-1]   su=ar[0]   arr=[]   arr.append(su)   for i in range(1,len(ar)):       su=su+ar[i]       arr.append(su)   arr=arr[::-1]   ans=tree(0,len(arr)-1,arr)   return ansdef inor(root,a):   if(root==None):       return   inor(root.left,a)   a.append(root.data)   inor(root.right,a)   return a   def tree(s,e,arr):   if(s>e):       return None   mid=(s+e)//2   root=Node(arr[mid])   root.left=tree(s,mid-1,arr)   root.right=tree(mid+1,e,arr)   return root   
0
forcer
This comment was deleted.
+1
badgujarsachin832 months ago
void solve(Node* root,int& sum){
    if(root==NULL){
        return ;
    }
    solve(root->right,sum);
    sum+=(root->data);
    root->data=sum;
    solve(root->left,sum);
}
// modify the BST and return its root
Node* modify(Node *root)
{
    // Your code here
    int sum=0;
    solve(root,sum);
    return root;
}
0
madhukartemba2 months ago
JAVA SOLUTION:
class Solution
{
    int sum = 0;
    public void rec(Node root)
    {
        if(root==null) return;
        
        rec(root.right);
        
        root.data += sum;
        sum = root.data;
        
        rec(root.left);
        
        
    }
    
    
    // modify the BST and return its root
    public Node modify(Node root)
    {
        sum = 0;
        
        rec(root);
        
        return root;
        
    }
    
}
0
sohamdas86972 months ago
void solve(Node* root, int& sum)
{
    if(root == NULL)
        return;
    solve(root->right, sum);
    sum += (root->data);
    root->data = sum;
    solve(root->left, sum);
}
Node* modify(Node *root)
{
    int sum = 0;
    solve(root, sum);
    return root;
}
0
hamidnourashraf2 months ago
def _inorder(root, res):
    if root is None:
        return
    _inorder(root.left, res)
    res.append(root)
    _inorder(root.right, res)
    
def modify(root):
    res = []
    _inorder(root, res)
    for i in range(len(res)-2, -1, -1):
        res[i].data += res[i+1].data
    return root
+2
himanshukug19cs2 months ago
java solution
int sum=0;   void sol(Node root){       if(root==null)          return;       sol(root.right);       sum=root.data+sum;       root.data=sum;       sol(root.left);             }   public Node modify(Node root)   {       //Write your code here       sol(root);       return root;                 }
+1
shyamprakash8072 months ago
Simple Python solution:
 
def modify(root):   x = [0]   help(root, x)   return root
def help(root, x):   if root == None:       return   help(root.right, x)   root.data = root.data + x[0]   x[0] = root.data   help(root.left, x)
0
yanoop2382 months ago
void tree(Node*  & root , int & a){   if(root==NULL){       return ;   }      tree(root->right,a);   root->data+=a;   a=root->data;   tree(root->left,a);}
Node* modify(Node *root){   int a=0;   tree(root,a);      return root;// Your code here}
0
sanisettymanoj2 months ago
O(n) recursive 
static int sum = 0;
    public Node modify(Node root)
    {
        sum = 0;
        sumEach(root);
        return root;
    }
    public static void sumEach(Node root){
        if(root == null)
        return;
        sumEach(root.right);
        sum +=root.data;
        root.data = sum;
        sumEach(root.left);
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.