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:

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:

Run above program and you will get following output:

Java Binary tree tutorial:


import_contacts

You may also like:

Related Posts

  • Print blank line in Python
    23 September

    Print blank line in Python

    Table of ContentsWhat is a blank line in Python?Ways to print blank line in PythonUsing an empty print() functionUsing the print() function with newline characterUsing the print() function with an empty stringUsing the print() function with the sep parameterWays to print multiple blank lines in PythonUsing the print() function with multiple newline charactersUsing the print() […]

  • 17 September

    Spectrogram in Python

    Table of ContentsSpectrogramPlotting Spectrogram using the matplotlib.pyplot.specgram function of the Matplotlib Library.Plotting Spectrogram using the scipy.signal.spectrogram function of the SciPy Library Spectrogram A spectrogram is a wave-like graph which is used to represent measures like loudness, frequencies, and other signals that change over time. With the help of a spectrogram, these signals and measures are […]

  • 17 September

    27 Best and Safe ROM Sites in 2021

    Table of ContentsThe Best and Safe ROM Sites in 2021DopeROMsCOOL ROMROMsmaniaGamulatorROM HustlerRetrosticROMspediaROMs WorldROM Nation ROMUIationROMs UniverseClassic GameROMsCDROMance Emulator Game     EmuparadiseWoWROMsEmulator ZoneNitroblogVimm’s LairFree ROMsOther Working Safe Rom Sites in 2021Frequently Asked Questions Are there any free and safe ROM sites?What are the safest ROM sites?Is it legal to download a ROM?Wrapping Up Do you still wish to play […]

  • 17 September

    Write list to CSV in Python

    Table of ContentsUsing the write() function to write a list to a CSV fileUsing the csv module to write a list to a CSV fileUsing the numpy module to write a list to a CSV fileUsing the numpy.savetxt() function to write a list to a CSV fileUsing the numpy.tofile() function to write a list to […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • 11 September

    RSA Encryption and Decryption in Java

    Table of ContentsIntroductionGenerate RSA key pairEncrypt a random textDecrypt the random textConclusion Introduction RSA is a short form for Rivest, Shamir, and Adleman, are the people who first publicly described it in 1977. It is an algorithm for asymmetric cryptography which involves the use of two keys. A public key, which can be known to […]

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.