Find the Height of Binary Tree

Height of the Binary Tree

A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent. The height of a binary tree is defined as the number of edges between the root node and the farthest leaf node.The height of an empty tree is 0.

construct a tree from preorder and postorder traversal

Find the Height of Binary Tree

Algorithm

Height of the binary tree can be easily calculated using recursion.

Height of tree= Maximum (Height of left subtree +Height of right subtree)  + 1 ( For the node itself ).

Now the left and right subtree height can be calculated with the help of recursion.

Example

Height of binary tree is the number of edges between’s tree root and its farthest leaf. A tree containing a single node has height 0.

Example to find height of binary tree

For this tree, height is 3. It is obtained with the help of recursion. Each subtree gives its height  and at last we get the height  of  the complete tree. It is explained as:

  1. height(50) is called initially. It calls height(25) and height(75)
  2. height(25) calls height(12) and height(37)
  3. height(12) returns 0 and height(37) calls height(30) (returns 0) and height(40) (returns 0). So height(37) returns max (0,0) + 1 i.e 1
  4. Now height(25) returns max(0,1) + 1 i.e.2. This is height of left subtree of 50
  5. In the similar way, we get height of right subtree of 50. The max would be 2.
  6. Now height of the tree would be-  max ( left subtree height i.e. 2 , right subtree height i.e. 2 ) + 1(for the node itself) = 3
  7. Hence height of the tree is 3.

It is explained as follows:

Heigth of Tree

Java Program to find height of the Tree

Run

import java.util.*;

public class Main
{

// Binary tree class
  public static class BinaryTree
  {
// Node class
    public class Node
    {

      int data;
      Node left;
      Node right;

      public Node (int data)
      {
	this.data = data;
	this.left = null;
	this.right = null;
      }
    }

    private Node root;

    public BinaryTree (int[]pre, int[]post)
    {
      this.root =
	this.construct (pre, 0, pre.length - 1, post, 0, post.length - 1);
    }

    private Node construct (int[]pre, int presi, int preei, int[]post,int postsi, int postei)
    {

// this case occurs when a node has only one child
      if (presi > preei)
	{
	  return null;
	}

      Node node = new Node (pre[presi]);
      node.left = null;
      node.right = null;

      if (presi == preei)
	{
	  return node;
	}

//Searching pre[presi + 1] in postorder array
      int pos = -1;
      for (int i = postsi; i <= postei; i++)
	{
	  if (post[i] == pre[presi + 1])
	    {
	      pos = i;
	      break;
	    }
	}

//Number of elements in left subtree
      int clc = pos-postsi + 1;

//Left subtree
      node.left =
	this.construct (pre, presi + 1, presi + clc, post, postsi, pos);

//Right subtree
      node.right =
	this.construct (pre, presi + clc + 1, preei, post, pos + 1,postei - 1);

      return node;
    }

    public int height ()
    {
      return this.height (this.root);
    }

//Function to find height of binary tree
    private int height (Node node)
    {

//Base case
      if (node == null)
	{
	  return -1;
	}

//Calculate height of left subtree
      int lht = this.height (node.left);

//Calculate height of right subtree
      int rht = this.height (node.right);

//Height of the tree is max of left and right subtree height + 1 (for the node itself)
      int rv = Math.max (lht, rht) + 1;
      return rv;
    }
  }

  public static void main (String[]args) throws Exception
  {

// Construct binary tree

    int[] pre = { 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87 };
    int[] post = { 12, 30, 40, 37, 25, 60, 70, 62, 87, 75, 50 };

    BinaryTree bt = new BinaryTree (pre, post);
    System.out.println ("Height of the tree is : "+bt.height ());

  }
}

Output:

Height of the Tree is : 3

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java