• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Print vertical sum of all the axis in the given binary tree

July 15, 2014 by Dhaval Dave

Print Vertical Sum of all the axis in the given binary Tree.

Most of times in written test , you will encounter this very good question.
given a binary tree.
(most of times a complete binary tree with all leaf node at same height will be given)
example :

                      1 A
                    /   \
                  /       \
                 /          \
             2 B              3 C
             /  \             /   \ 
           /      \         /       \ 
         4 D       5 E    6 F       7 G

For the above tree, Vertical sum should be calculated as follows,

Line 1: 4
Line 2: 2
Line 3: 1,5,6 =12
Line 4: 3
Line 5: 7
so Code should print 4,2,12,3,7

Adapt thinking pattern.

we need H alone, B,I and J should be together and so on…
so B,J,I should have same order/numbering or something…
First thought comes that we need height of tree, from there we can have some idea.
See when we print in order..it gives
4,  2,  5,1,6,  3,  7

Hence we can adapt In-Order traversal with some clustering/numbering so that we can print total with grouping

if you take height 3, see that if we multiply it by 2 ( as we have left and right subtrees)
we get 6.
In In-Order traversal when we start from root, we pass level 8.
At every left subtree call we can reduce it by one, and at right subtree call we can increase.
and hence what we get..

Inorder : 4,  2,  5,1,6,  3,  7
level :     4,  5 , 6,6,6 , 7 , 8

Hence we concluded that  We can sum all node->data who has same level.

Code :

void Vsum(struct node *temp, int level, int count[])
{
    if(temp)
    {
        Vsum(temp->left, level-1, count);
        count[level] += temp->data;
        Vsum(temp->right, level+1, count);
    }
    return;
}

int height(struct node * root)
{
struct node *temproot = root;
int i=0,j=0;
    while(temproot->left != NULL)
    {
        i++;
        tmproot = tmproot->left;
    }
    while(temproot->right != NULL)
    {
        j++;
        tmproot = tmproot->left;
    }    
 if(i>j) return 2*i ;
 else return 2*j ;
}

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Tree, tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Linked List V/S Binary Search Tree

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Python Dictionaries

Implement LRU Cache

Given a string, find the first character which is non-repetitive

simple sql injection

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Generate next palindrome number

Reversal of LinkedList

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

BFS (Breath First Search)

Naurki.com Security Breach

Word Break Problem

Maximum occurred Smallest integer in n ranges

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

The Magic HackerEarth Nirvana solutions Hiring Challenge

Doubly linked list

Coin Collection Dynamic Programming

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Flipkart SDET Interview Experience

Find min element in Sorted Rotated Array (Without Duplicates)

Difference between a LinkedList and a Binary Search Tree BST

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Maximum of all subarrays of size k

Find and print longest consecutive number sequence in a given sequence in O(n)

Find if two rectangles overlap

Check if an array has duplicate numbers in O(n) time and O(1) space

Copyright © 2025 · Genesis Framework · WordPress · Log in