Table of Contents
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.
Run above program and you will get following output:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// prints vertical sum of binary tree public static void printVertivalSumOfBinaryTree(TreeNode startNode,TreeMap<Integer,Integer> 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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<Integer,Integer> 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<Integer,Integer> treeNodeMap=new TreeMap<Integer,Integer>(); printVertivalSumOfBinaryTree(rootNode, treeNodeMap, 0); for(Entry<Integer,Integer> 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; } } |
1 2 3 4 5 6 7 8 |
Vertical sum of binary tree will be: 10 25 120 115 70 |
Java Binary tree tutorial:
- Binary tree in java
- Binary tree preorder traversal
- Binary tree postorder traversal
- Binary tree inorder traversal
- Binary tree level order traversal
- Binary tree spiral order traversal
- Binary tree reverse level order traversal
- Binary tree boundary traversal
- Print leaf nodes of binary tree
- Count leaf nodes in binary tree
- get maximum element in binary tree
- Print all paths from root to leaf in binary tree
- Print vertical sum of binary tree in java
- Get level of node in binary tree in java
- Lowest common ancestor(LCA) in binary tree in java
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.