Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Print the Vertical Sum in binary Tree .

Objec­tive: - Given a binary tree, print it in ver­ti­cal order sum

What is Ver­ti­cal Order Sum



as you can see in the exam­ple above, [4],[2], [12],[3],[7] are the ver­ti­cal order sum of the given binary tree.


  • Do the inorder traversal.
  • Take a vari­able called level, when ever you fo left, do level++ AND when ever you go right do level–.
  • With step above we have sep­a­rated out the lev­els vertically.
  • Now you need to add the ele­ments of each level, so cre­ate a TreeMap and the (key,value) pair will be (level,Sum of elements).
  • At the end iter­ate through the TreeMap and print the results.



Com­plete Code:


Level -2 Sum : 7 
Level -1 Sum : 3 
Level 0 Sum : 12 
Level 1 Sum : 2 
Level 2 Sum : 4

You may also like...