Find maximum element in binary tree in java

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm 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.

Join Our Newsletter

Subscribe to Awesome Java Content

Add Comment

Join Our Newsletter

Subscribe to Awesome Java Content