Find maximum element in 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 11th part of java binary tree tutorial.

In this post, we will see about program to find maximum element  in a binary tree in java.
There can be two solutions for it.

  • Recursive
  • Iterative

Recursive solution:

Algorithm :

Steps for getting maximum element in binary tree:
  • Find maximum element in left subtree
  • Find maximum element in right subtree
  • Compare maximum of above subtrees to current node
  • We will find maximum element with above steps

Code for recursion will be:

Iterative solution:

Iterative solution will be similar to level order traversal. When we are popping element from queue, we will check max.

Code for iteration will be :

Lets create java program to get maximum element in binary tree:

Lets say, your binary tree is this:

Run above program and you will get following output:

Java Binary tree tutorial:

Please go through java interview programs for more such programs.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Array of HashTables in PowerShell
    02 February

    PowerShell – Array of HashTables

    Table of ContentsCreating an Array of HashTablesPassing Data from a Parameter to an Array of HashTable Creating an Array of HashTables To create an array of hashtables in PowerShell: Create an array. Create two or more hash tables. Use += operator to add the hash tables (created in the second step) to an array (created […]

  • Write Binary Files in PowerShell
    01 February

    Write Binary Files in PowerShell

    Table of ContentsUsing Add-Content CmdletUsing the [IO.File]::WriteAllBytes() MethodUsing FromBase64CharArray() with Add-Content Using Add-Content Cmdlet Use the Add-Content cmdlet to write binary files in PowerShell. [crayon-63db09f4e9202122231966/] This script created a new file named test.bin in the current working directory and wrote the binary data to the file. The script will overwrite the existing file with the […]

  • 01 February

    What Does Percent(%) Mean in PowerShell

    Table of ContentsUsing Percent (%) in Different ContextsUse Percent(%) as an alias of the ForEach-Object cmdlet in PowerShellUse percent (%) as the Modulus Operator in PowerShellUse percent(%) as an assignment operator (%=) in PowerShell Using Percent (%) in Different Contexts Typically the % character is for mod functionality. But in PowerShell, it’s an alias for […]

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63db09f4ea1e9174528504/] [crayon-63db09f4ea1ed849424216/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

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.