This post is completed by 1 user
|Add to List|
Print the Vertical Sum in binary Tree .
Objective: - Given a binary tree, print it in vertical order sum
What is Vertical Order Sum
as you can see in the example above, ,, ,, are the vertical order sum of the given binary tree.
- 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