Height Of A Binary Tree in C++

Height Of The Tree

The height of a binary is 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 and height of the tree given in the image is 3.

Height of a binary tree in cpp

Common Uses Of Finding Height Of Tree

  1. To check whether the tree is an AVL tree or not. An AVL tree is a binary search tree in where the difference of left and right subtree is less than or equal to one for all nodes.
  2. To Print Level Order Traversal using recursion.
  3. The number of nodes in a perfect binary tree can be determined using height of the tree.

Algorithm:

  1. If root is NULL, return 0.
  2. Otherwise Recursively call the height function on its left and right child.
  3. Return the value which is greater out of height of left child and bight of right child
Find the height of binary tree in C

Code Implementation to find height of a Binary Tree in C++

Run
#include<bits/stdc++.h>
using namespace std;

struct Node
{
  int data;
  Node *left;
  Node *right;
};

// Function to create a new node
Node * newNode (int data)
{
  Node *node = new Node;
  node->data = data;
  node->left = nullptr;
  node->right = nullptr;
  return node;
}

// Function to find the height of a binary tree
int height (Node * root)
{
  if (root == nullptr)
    {
      return 0;
    }
  else
    {
      int left_height = height (root->left);
      int right_height = height (root->right);
      return 1 + max (left_height, right_height);
    }
}

int main ()
{
  // Create a binary tree
  Node *root = newNode (1);
  root->left = newNode (2);
  root->right = newNode (3);
  root->left->left = newNode (4);
  root->left->right = newNode (5);

  // Find the height of the binary tree
  cout << "Height of the binary tree is " << height (root) << endl;

  return 0;
}

Output:
Height of the Binary Tree is 3

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One 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

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