Vertical sum | Practice | GeeksforGeeks

Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines starting from left-most vertical line to right-most vertical line.
Example 1:
Input:
       1
    /   \
  2      3
 / \    / \
4   5  6   7
Output: 
Explanation:
The tree has 5 vertical lines
Vertical-Line-1 has only one node
4 => vertical sum is 4
Vertical-Line-2: has only one node
2=> vertical sum is 2
Vertical-Line-3: has three nodes:
1,5,6 => vertical sum is 1+5+6 = 12
Vertical-Line-4: has only one node 3
=> vertical sum is 3
Vertical-Line-5: has only one node 7
=> vertical sum is 7

Your Task:
You don't need to take input. Just complete the function verticalSum() that takes root node of the tree as parameter and returns an array containing the vertical sum of tree from left to right.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1<=Number of nodes<=1000
0
akashkhurana282 days ago
JAVA SOLUTION
void vsum(Node root, int hd,TreeMap<Integer,Integer>mp){       if(root==null)return;       vsum(root.left,hd-1,mp);       int psum=(mp.get(hd)==null)?0:mp.get(hd);       mp.put(hd,psum+root.data);       vsum(root.right,hd+1,mp);   }   public ArrayList <Integer> verticalSum(Node root) {       // add your code here       TreeMap<Integer,Integer>mp=new TreeMap<Integer,Integer>();       vsum(root,0,mp);       ArrayList<Integer> res= new ArrayList<Integer>();      for(int i : mp.keySet()){       res.add(mp.get(i));       }       return res;   }} 
+1
harshscode1 week ago
void verti_sum(Node *root,map<int,int> &m,int hd) {     if(root==NULL)     return;          verti_sum(root->left,m,hd-1);     m[hd]+=root->data;     verti_sum(root->right,m,hd+1); }   vector <int> verticalSum(Node *root) {       map<int,int> m;       verti_sum(root,m,0);              vector<int> v;       for(auto i:m)       v.push_back(i.second);              return v;   }
+1
salona23
This comment was deleted.
0
yoginpcs202 weeks ago
void recur(Node * root,map<int,int> &mp,int hd)
   {
       if(root==NULL)
       return;
       recur(root->left,mp,hd-1);
       mp[hd]+=root->data;
       recur(root->right,mp,hd+1);
   }
    vector <int> verticalSum(Node *root) {
        // add code here.
        map<int,int> mp;
        recur(root,mp,0);
        vector<int> v;
        
        for(auto i:mp)
        {
            v.push_back(i.second);
        }
        return v;
    }
 
+1
badgujarsachin833 weeks ago
   vector <int> verticalSum(Node *root) {

        map<int,int> mp;
        queue<pair<Node*,int>> q;
        q.push({root,0});
        while(!q.empty()){
            auto temp=q.front();
            q.pop();
            Node* ans=temp.first;
            int dis=temp.second;
            int v=mp[dis];
            mp[dis]=v+ans->data;
            if(ans->left){
                q.push({ans->left,dis-1});
                
            }
            if(ans->right){
                q.push({ans->right,dis+1});
            }
        }
        vector<int> v;
        for(auto it:mp){
            v.push_back(it.second);
        }
        return v;
    }
-1
dadwalabhishek101 month ago
class Solution{
  public:
    map<int,int> mp;
    void dfs(Node* root, int val)
    {
        if(!root) return;
        mp[val] += root->data;
        dfs(root->left, val-1);
        dfs(root->right, val+1);
    }
    vector <int> verticalSum(Node *root) {
        // add code here.
        dfs(root, 0);
        vector<int> res;
        for(auto i:mp) res.push_back(i.second);
        return res;
    }
};
-1
iliyazali442 months ago
python solution easy
 
 
def verticalSum(self, root):             d={}              def helper(root,d,hd):           if not root:               return           if hd in d:               d[hd]=d[hd]+root.data           else:               d[hd]=root.data           helper(root.left,d,hd-1)           helper(root.right,d,hd+1)       helper(root,d,0)       ans=[]       for i,j in sorted(d.items()):           ans.append(j)       return ans  
0
abhixhek052 months ago
 vector <int> verticalSum(Node *root) {
      
        map<int,int> mp;
        queue<pair<Node*,int>>q;
        q.push({root,0});
        while(q.size()){
            
            auto a = q.front();
            q.pop();
            Node* curr = a.first;
            int val = a.second;
            int v = mp[val];
            mp[val]= v + curr->data;
            
            if(curr->left)q.push({curr->left, val-1});
            if(curr->right)q.push({curr->right, val+1});
        }
       vector<int>res;
        for(auto it: mp){
            res.push_back(it.second);
        }
        
        return res;

    }
-1
amanpandey30072 months ago
// Avoid using ordered_map by keeping track of mn_c(min column) and mx_c
 
void solve(Node*root,unordered_map<int,int>&m,int c,int &mx_c,int &mn_c)   {       if(!root)       return;              mx_c=max(mx_c,c);       mn_c=min(mn_c,c);              m[c]+=root->data;              solve(root->left,m,c-1,mx_c,mn_c);       solve(root->right,m,c+1,mx_c,mn_c);          }   vector <int> verticalSum(Node *root)    {       unordered_map<int,int>m;       vector<int>ans;       int mx_c=-1;       int mn_c=100;              solve(root,m,0,mx_c,mn_c);                            for(int i=mn_c;i<=mx_c;i++)       {           ans.push_back(m[i]);       }              return ans;   }
-1
sumandas203 months ago
 void helper(Node*root,map<int,int>&m){     queue<pair<Node*,int>>q;     q.push({root,0});     while(!q.empty()){         auto p = q.front();         q.pop();         Node*t=p.first;         int level=p.second;         if(m.find(level)==m.end()){             m[level]=t->data;         }         else{             m[level]+=t->data;         }         if(t->left!=NULL)         q.push({t->left,level-1});         if(t->right!=NULL)         q.push({t->right,level+1});     } }   vector <int> verticalSum(Node *root) {       // add code here.       vector<int>ans;       if(root==NULL)return ans;       map<int,int>m;       helper(root,m);       for(auto i:m){           ans.push_back(i.second);       }       return ans;   }
 
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.