Last Updated: 17 Dec, 2020

Vertical Sum in a Binary Tree

Hard
Asked in companies
HSBCOracleSnapdeal Ltd.

Problem statement

Given a binary tree having a positive integer written on each of its nodes. Your task is to find the vertical sum of node values i.e. the sum of nodes that can be connected by a vertical line.

A binary tree is a tree in which each parent node has at most two children.

For example:
Consider the following Binary Tree:

sample-tree

So the final answer is
12 9 11 6
Input format
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The first line of each test case contains the elements of the tree in the level order form separated by a single space. If any node does not have a left or right child, take -1 in its place.
For example, the input for the tree depicted in the below image would be :

alt text

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output format:
For each test case, print the vertical sum of node values separated by a space in a separate line.
Constraints:
1 <= T <= 100
1 <= N <= 3000
1 <= node data <= 10^9

Time Limit: 1sec     

Note:

You are not required to print the output explicitly as it has already been taken care of. Just implement the function and return the answer, which is the sequence of summation of nodes connected by the same vertical line starting from left to right.

Approaches

01 Approach

First, we see below example before discussing the approach:

*

For the above given binary tree:

1 is the root node, we say its horizontal distance = 0.

Since 2 lies to the left of 1, its horizontal distance from 1 is 0-1= -1

3 lies to the right of 1, its horizontal distance = 0+1 = 1

Similarly, horizontal distance of 4 = Horizontal distance of 2 - 1= -1-1=-2

Horizontal distance of 5 = Horizontal distance of 2 + 1=  -1+1 = 0

Horizontal distance of 6 = Horizontal distance of 3 - 1 i.e 1-1 =0

Horizontal distance of 7 = Horizontal distance of 3 +1 i.e 1+1 = 2

So Nodes at horizontal distance -2 = 4 , Sum=4,

Nodes at horizontal distance -1 = 2 , Sum=2,

Nodes at horizontal distance  0 = 1,5,6 ,Sum=12 (1+5+6),

Nodes at horizontal distance 1= 3 , Sum=3,

And Nodes at horizontal distance 2= 7, Sum=7

So the final answer is

4 2 12 3 7

So we use a map having {horizontalDistance,sumOfNodes} as the key-value pair. Now on traversing the tree, we check whether the current calculated horizontal distance is present on the map. If it is present on the map, we increment that key’s value by the node value. Otherwise, we make a new key, value pair in the map having current horizontal distance as the key and the node value as its value. Then we move on to traverse the remaining subtree. We have to pass the horizontal distance as an argument while traversing the tree.

Note that the horizontal distance gets increased by 1 when we move to a right child whereas it gets decreased by 1 when we move to a left child.

When the tree is completely traversed, we traverse the map and print the map values in the order of least horizontal distance value to the greatest horizontal distance value. We will use an ordered map as we need to print the sequence in the sorted order of horizontal distance.

02 Approach

The idea is to use a Doubly Linked list (DLL) in which nodes are ordered according to the horizontal distance of the nodes from the root and store the sum of values of nodes at the same horizontal distance in the same node of Doubly Linked List.

This approach is similar to the previous approach but in this, we will be storing the sum of values in a Doubly Linked List and not in a map.

We need to use a helper function to traverse the Binary Tree having the pointer to the current node of the binary tree and the current node of the DLL as the argument.

Note that DLL_Node is the current DLL node whereas TreeNode is the current Binary Tree node. 

In our helper function, we will first increment DLL_Node value by TreeNode value.

The remaining working of the helper function can be divided into 4 cases:

  1. If Treenode-> left !=NULL
    1. If (DLL_Node)->prev=NULL
      1. We create a new Node having value 0 and insert it into the behind DLL_Node and call helper function for TreeNode->left and (DLL_node)->prev
    2. If (DLL_Node)->prev!=NULL, we call the helper function for  TreeNode-> left and ( DLL_Node)->prev.
  2. If Treenode->right!=NULL
    1. If (DLL_Node)->next=NULL
      1. We create a new Node having value 0 and insert it into the DLL ahead of DLL_Node and call helper function for TreeNode->right and (DLL_node)->next
  3. if (DLL_Node)->next!=NULL

We call helper function for  TreeNode-> right and ( DLL_Node)->next 

 

             

In our primary function, we need to create a new DLL node and will pass it to our helper function. After running the helper function, we will start traversing from the start of  DLL and print its node values i.e our vertical sums.