Print the Vertical Sum in binary Tree .
Objective: – Given a binary tree, print it in vertical order sum
What is Vertical Order Sum
- Do the inorder traversal.
- Take a variable called level, when ever you fo left, do level++ AND when ever you go right do level–.
- With step above we have separated out the levels vertically.
- Now you need to add the elements of each level, so create a TreeMap and the (key,value) pair will be (level,Sum of elements).
At the end iterate through the TreeMap and print the results.
Level -2 Sum : 7 Level -1 Sum : 3 Level 0 Sum : 12 Level 1 Sum : 2 Level 2 Sum : 4
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.