DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree

We have a binary tree structure and we need to find its height. It is defined by the distance from the root to the last downward node.

Here the height is 2.

Lets understand it with another example:

The height of the above tree is 3.

Approach

Recursively find the height of the left and right subtrees, and a maximum of two will be the height of the tree.

Pseudo code

1. If tree == NULL then, return 0

2. Else

            (a) call maxDepth( tree->left-subtree) recursively.

            (a) call maxDepth( tree->right-subtree) recursively.

            (c) After we get maximum depths -

            max_depth = max(max dept of left subtree,  max depth of right subtree) + 1

            (d) Return max_depth as height of tree.

C++ Code:

 #include <bits/stdc++.h>
 using namespace std;
 // Create tree structure
 class node {
 public:
     int data; // value of a node
     node* left; // left pointer to the tree
     node* right; // right pointer to the tree
 };
 // function to find the Height of the tree
 int maxDepth(node* node)
 {
     if (node == NULL) // if we reach root node
         return 0;
     else {
         int lDepth = maxDepth(node->left); // find height of left subtree
         int rDepth = maxDepth(node->right); // find height of the right subtree
         // return the largest one
         if (lDepth > rDepth)
             return (lDepth + 1); // if left subtree height is greater return its height + 1
         else
             return (rDepth + 1); // if right subtree height is greater return its height + 1
     }
 }
 // A function to insert data in to the tree
 node* newNode(int data)
 {
     node* Node = new node(); // Allocate memory for node
     Node->data = data; // Assign value to the tree
     Node->left = NULL; // Make left pointer NULL
     Node->right = NULL; // Make right pointer NULL
     return (Node); // return that Node
 }
 // Main function
 int main()
 {
     node* root = newNode(1); // root of the tree
     root->left = newNode(2); // the left of root
     root->right = newNode(3); // the right of root
     cout << "Height of tree is " << maxDepth(root); // call function to find Height
     return 0;
 } 

C Code:

 #include <stdio.h>
 #include <stdlib.h>
 // Create tree structure
 struct node {
     int data; // the data to the node
     struct node* left; // the left pointer to the node
     struct node* right; // the right pointer to the node
 };
 int maxDepth(struct node* node) // function to find the Height of the tree
 {
     if (node == NULL) // if we reach root node
         return 0;
     else {
         int lDepth = maxDepth(node->left); // find height of left subtree
         int rDepth = maxDepth(node->right); // find height of the right subtree
         // return the largest one
         if (lDepth > rDepth)
             return (lDepth + 1); // In case left subtree is greater
         else
             return (rDepth + 1); // In case right subtree is greater
     }
 }
 // Allocate memory to the node
 struct node* newNode(int data)
 {
     struct node* node  = (struct node*)malloc(sizeof(struct node));
     node->data = data; // assign data
     node->left = NULL; // the left pointer is NULL
     node->right = NULL; // the right pointer is NULL
     return (node); // return current node
 }
 // Main function
 int main()
 {
     struct node* root = newNode(1); // create root node
     root->left = newNode(2); // create left child
     root->right = newNode(3); // create right child
     printf("Height of tree is %d", maxDepth(root)); // find height of the tree
        return 0;
 } 

Output:

Height of tree is 2

Time complexity:

O(n)

Related Topics

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

4 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have...

2 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data,...

3 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 minutes read.