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.


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:

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


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

Run above program and you will get following output:

Java Binary tree tutorial:

Was this post helpful?


You may also like:

Related Posts

  • PowerShell get filename for Path
    03 January

    PowerShell – Get Filename from Path

    Table of ContentsPowerShell – Get Filename From Specified PathUsing Split-Path CmdletUsing Get-Item CmdletUsing Get-ChildItem CmdletUsing GetFileName() MethodGetting Multiple File NamesWith ExtensionsWithout Extension PowerShell – Get Filename From Specified Path The location of a file on a system can be determined by its path. In PowerShell, there are several ways to get filename from path. Firstly, […]

  • Count files in folder in PowerShell
    01 January

    PowerShell – Count Files in Folders

    Table of ContentsCount Files in Folders in PowerShellCount Files in multiple Folders in PowerShellCount Files in Folders and Subfolders in PowerShellCount only folders inside folder in PowerShellCount Files by Extension in Folder in PowerShell Count Files in Folders in PowerShell We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, […]

  • Check if Object contains property in PowerShell
    31 December

    Check if Object has Property in PowerShell

    Table of ContentsUsing the -match ParameterUsing -contains ParameterUsing if-else BlockUsing .Match() Function with if-else BlockUsing Get-Member Cmdlet Using the -match Parameter Use the -match parameter to check if an object has a property in PowerShell. [crayon-63b4efb6140aa182336423/] [crayon-63b4efb6140ad375855130/] Here, we used the -match parameter to check if the given object, which is $result in our case, […]

  • Create Empty File in PowerShell
    28 December

    Create Empty File in PowerShell

    Table of ContentsUsing New-Item CmdletUse the ni AliasUsing Out-File CmdletUsing the fsutil file CommandUsing Text EditorUsing echo CommandUsing for Loop Using New-Item Cmdlet Use the New-Item cmdlet to create an empty .txt file in PowerShell. [crayon-63b4efb61423e672738526/] [crayon-63b4efb614240615115312/] We used the New-Item cmdlet with -Path, -Name, and -ItemType parameters to create an empty .txt file using […]

  • 27 December

    Run PowerShell as Another User

    Table of ContentsUsing the runas CommandOpen the PowerShell as an AdministratorRun the runas commandUsing the Start-Process cmdlet with the Credential ParameterUI-Based SolutionScript-Based Solution Using the runas Command The runas command in PowerShell launches programs using credentials different from the current user. It provides users with limited privileges to execute commands or access resources available only […]

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b4efb614507995277173/] [crayon-63b4efb61450a222043419/] We used numpy.empty() […]

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.