Vertical sum of binary tree in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

This is 10th part of java binary tree tutorial.

In this post, we will see how to print vertical sum of binary tree nodes in java. Below diagram will show vertical sum for binary tree.

Algorithm:

Steps for print vertical sum of binary tree:
  • Traverse tree in inorder traversal.
  • Create a variable level and initialise it with 0. When you traverse left child, decrease level by 1(level–) and when you traverse right child, increase level by 1(level++).
  • We need to maintain TreeMap with key as level and value as node data. If you get same key(level) again, then you need to add current node data to previous stored value to calculate sum.
    For example:
    TreeMap has entry with (0,40) where 0 is level and 40 is node data. So while traversing, if you encountered node 30 at level 0, so after processing node 30, TreeMap will have entry as (0,70)
  • Once TreeMap is populated after iterating all nodes, print the results.

Code for recursion will be:

// prints vertical sum of binary tree
    public static void printVertivalSumOfBinaryTree(TreeNode startNode,TreeMap treeNodeMap,int level) {
        if(startNode==null)
        {
            return;
        }

        // Decrease level by 1 when iterating left child
        printVertivalSumOfBinaryTree(startNode.left,treeNodeMap,level-1);

        if(treeNodeMap.get(level)!=null)
        {
            Integer sum=treeNodeMap.get(level)+startNode.data;
            // Adding current node data to previous stored value to get the sum
            treeNodeMap.put(level, sum);
        }
        else
        {

            treeNodeMap.put(level, startNode.data);
        }
        // Increase level by 1 when iterating left child
        printVertivalSumOfBinaryTree(startNode.right,treeNodeMap,level+1);

    }

Please find diagram below which shows level assigned for each binary tree node.

Example:

Lets create java program for printing vertical sum in binary tree:

 

package org.arpit.java2blog.binarytree;

import java.util.Map.Entry;
import java.util.TreeMap;

public class BinaryTreeVerticalSumMain {

    public static class TreeNode
    {
        int data;
        TreeNode left;
        TreeNode right;
        TreeNode(int data)
        {
            this.data=data;
        }
    }

    // prints vertical sum of binary tree
    public static void printVertivalSumOfBinaryTree(TreeNode startNode,TreeMap treeNodeMap,int level) {
        if(startNode==null)
        {
            return;
        }

        // Decrease level by 1 when iterating left child
        printVertivalSumOfBinaryTree(startNode.left,treeNodeMap,level-1);

        if(treeNodeMap.get(level)!=null)
        {
            Integer sum=treeNodeMap.get(level)+startNode.data;
            // Adding current node data to previous stored value to get the sum
            treeNodeMap.put(level, sum);
        }
        else
        {

            treeNodeMap.put(level, startNode.data);
        }
        // Increase level by 1 when iterating left child
        printVertivalSumOfBinaryTree(startNode.right,treeNodeMap,level+1);

    }
    public static void main(String[] args)
    {
        // Creating a binary tree
        TreeNode rootNode=createBinaryTree();
        System.out.println("Vertical sum of binary tree will be:");
        TreeMap treeNodeMap=new TreeMap();
        printVertivalSumOfBinaryTree(rootNode, treeNodeMap, 0);

        for(Entry entry:treeNodeMap.entrySet())
            System.out.println(entry.getValue());
    }

    public static TreeNode createBinaryTree()
    {

        TreeNode rootNode =new TreeNode(40);
        TreeNode node20=new TreeNode(20);
        TreeNode node10=new TreeNode(10);
        TreeNode node30=new TreeNode(30);
        TreeNode node60=new TreeNode(60);
        TreeNode node50=new TreeNode(50);
        TreeNode node70=new TreeNode(70);
        TreeNode node55=new TreeNode(55);
        TreeNode node5=new TreeNode(5);

        rootNode.left=node20;
        rootNode.right=node60;

        node20.left=node10;
        node20.right=node30;

        node60.left=node50;
        node60.right=node70;
        node50.right=node55;
        node30.left=node5;
        return rootNode;
    }
}

Run above program and you will get following output:

 
Vertical sum of binary tree will be:
10
25
120
115
70

Java Binary tree tutorial:

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.