[AMAZON] Vertical sum of nodes

Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples:
    /   \
  2      3
 / \    / \
4   5  6   7
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
So expected output is 4, 2, 12, 3 and 7


  1. A Binary tree can have all the elements aligned in a linear way.so if we assign root as vertical line 1 then its left child will be -1 and right will be +1.so either create a map stl and update the sum at each level corresponding to its own line number.

    get_Vertical_Sum(Tree* t,int level)

  2. A better solution without using stl is create an array of 2*h+1 elements where h is height of tree, then rest logic will be same.

  3. Why we will create an array of size 2*h+1.it also invlolves finding the height of tree.
    Better approach may be->
    Find the total no. of vertical lines this way ->
    Use Inorder DFS to find the distance of leftmost node from root and distance of rightmost node from root
    Total vertical lines= leftdistance + 1 + rightdistance

    Now fill the array using same logic of +1,-1.
    Time Complexity still remains - O(n)

  4. 1
    / \
    2 3
    / \ / \
    4 5 6 7
    in this case height of tree is 3..but number of vertical lines are same .so in this case 2*h+1 = 2*3+1 =7 which will give grabage value.

  5. 8 is left child of 5.

  6. @navin- left most +right most +1 ... will also not give exact number of vertical sum if 8 has left child 99 and 99 has left child 100 and 100 has left child 101.

  7. @Anonymous when you create an array of 2*h+1, initially fill all the index with value INT_MIN or NaN so that after traversing the elements you can know how many of them are filled with the values of nodes and how many of them are not visited...

    void get_Vertical_Sum(Tree* T,int i)
    if (verticalSum[i] == INT_MIN)
    verticalSum[i] = 0;

    1. sorry i am not getting it. my question was to related to size of verticalSum array. if we give its size 2*h+1 then in my above comment (in which 8 is left child of 5) it build total 2*3+1 =7 . and when we print value of verticalSum through loop.
      print value of verticalSum[i];

      it gives total 7 value but still we have only 5 value of verticalSum.please correct me if i am wrong.

    2. @Anonymous yeah you are right even though there will be 7 entries in the array, but all the entries will be initialized to INT_MIN, which we expect will not occur as a sum in the tree.so when you print the array you will skip those entries whose value is still INT_MIN.

    3. yeah..as i said we have to skip. actually i wanted to create a big enough memory to verticalSum, as use of space is always constraint.i just want to give array size equal to exact number of vertical lines.

    4. @Anonymous count the number of columns first then create an array of exactly max-min+1 size.This function basically counts the distance of left most node and right most node from root.Initially assign min = INT_MAX, max = INT_MIN and i = 0.Hope this clears ur doubt...please comment.
      void Count_Columns(Tree* pNode, int i, int& min, int& max)
      if (!pNode)
      Count_Columns(pNode->left, i-1, min, max);
      min = ::min(i, min);
      max = ::max(i, max);
      Count_Columns(pNode->right, i+1, min, max);

  8. @Navin please comment how will you find leftmost node and rightmost node...

  9. for leftmost and right most node.. we can use dfs .but it failed to count exact number of vertical sum .

  10. @All
    my question is you need to store the vetical sum in a doubly Linked List ? your function should be returning the head of doubly Linked List

    Thank you


Post a Comment

Popular posts from this blog