- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Write a Program to Find the Maximum Depth or Height of a Tree in C++

In this problem, we are given a binary tree. Our task is to write a program to find the maximum depth or height of the given tree.

Let’s take an example to understand the problem,

The height of the tree is 3.

To find the maximum height of a tree, we will check the heights of its left and right subtree and add one to the maximum of both. This is a recursive process that will continue this the last node is of the tree is found and one is added progressively to find the height of the sub-trees.

## Above example solved using this method.

Finding the height of tree i.e. height(3) = max(height(5),height(7)) + 1.

For this, we will calculate the height of nodes with values 5 and 7.

height(5) = max(height(1),height(9)) + 1 and

height(7) = 1, it has no subtree that can be formed.

Similarly, height(1) = height(9) = 1

height(5) = max(1,1) +1 = 2

height(3) = max(height(5),height(7)) + 1 = max(2,1) + 1 = 3.

So the height becomes 3.

Program to illustrate the solution,

## Example

#include <iostream> using namespace std; class node { public: int data; node* left; node* right; }; int height(node* node) { if (node == NULL) return 0; else { int lDepth = height(node->left); int rDepth = height(node->right); if (lDepth > rDepth) return(lDepth + 1); else return(rDepth + 1); } } node* insertNode(int data) { node* Node = new node(); Node->data = data; Node->left = NULL; Node->right = NULL; return(Node); } int main() { node *root = insertNode(4); root->left = insertNode(5); root->right = insertNode(0); root->left->left = insertNode(1); root->left->right = insertNode(9); cout<<"The height of the given binary tree is "<<height(root); return 0; }

## Output

The height of the given binary tree is 3

- Related Articles
- Find Minimum Depth of a Binary Tree in C++
- Maximum Depth of Binary Tree in Python
- C++ program to Replace a Node with Depth in a Binary Tree
- Program to check whether a tree is height balanced or not in C++
- Program to find the maximum width of a binary tree in Python
- Depth of an N-Ary tree in C++ Program
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Find maximum (or minimum) in Binary Tree in C++
- Write a program to Delete a Tree in C programming
- Write a program to Calculate Size of a tree - Recursion in C++
- Depth of the deepest odd level node in Binary Tree in C++ Program
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Minimum Depth of Binary Tree in C++
- Program to find maximum building height in Python
- Write a program in Python to find the maximum length of a string in a given Series