Vertical Sum in a given Binary Tree
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:
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 :
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.
CodingNinjas
author
2y
We need to check the Horizontal Distances from the root for all nodes. If two nodes have the same Horizontal Distance (HD), then they are on the same vertical line. The idea of HD is simple. HD for ro...read more
CodingNinjas
author
2y
Map Based 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 ...read more
CodingNinjas
author
2y
Doubly Linked List Based 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...read more
Add answer anonymously...
Top Snapdeal Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Snapdeal Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app