CARVIEW |
Select Language
HTTP/2 307
date: Sat, 11 Oct 2025 03:54:49 GMT
content-type: text/html
content-length: 66
location: https://how.dev/answers/finding-the-maximum-depth-of-a-binary-tree
cf-ray: 98cb6b192eba999b-BLR
cache-control: public, max-age=300, stale-while-revalidate=604800
referrer-policy: strict-origin-when-cross-origin
x-app-version: v251008-h-251010-1202
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-app-type: Learn
x-robots-tag: all
x-nextjs-cache: MISS
x-cloud-trace-context: f82179a0378b8f2738e3280319847398
via: 1.1 google
alt-svc: h3=":443"; ma=86400
cf-cache-status: EXPIRED
set-cookie: __cf_bm=SGu3sz1cDivdNr2yYaGwc0SNioc_HF8UB6.ryMMVTmo-1760154889-1.0.1.1-nAB7_FEpD4EUkcUvp9D1YjY21UqbpVijfbhQUuuQnZYhsn6FzUmm_Ral5AJIGeE3HUOb8Udog43bmY78q8ZivDOZWi.AKEeS3sKxb_WqR0I; path=/; expires=Sat, 11-Oct-25 04:24:49 GMT; domain=.educative.io; HttpOnly; Secure; SameSite=None
vary: Accept-Encoding
strict-transport-security: max-age=31536000; includeSubDomains; preload
server: cloudflare
HTTP/2 200
cache-control: public, max-age=300, stale-while-revalidate=604800
referrer-policy: strict-origin-when-cross-origin
x-app-version: v251008-h-251010-1202
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-app-type: Learn
x-middleware-rewrite: /answers/howdev/finding-the-maximum-depth-of-a-binary-tree
x-nextjs-cache: HIT
etag: W/"s2jf321ppc1fehj"
content-type: text/html; charset=utf-8
x-cloud-trace-context: f88804e304810b9042f2add66b4dd213
date: Sat, 11 Oct 2025 03:54:49 GMT
server: Google Frontend
via: 1.1 google
vary: Accept-Encoding
content-encoding: gzip
x-cache-status: miss
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Finding the maximum depth of a binary tree
Finding the maximum depth of a binary tree
The maximum depth of a binary tree is the number of nodes from the root down to the furthest leaf node. In other words, it is the height of a binary tree.
Consider the binary tree illustrated below:
The maximum depth, or height, of this tree is 4; node 7 and node 8 are both four nodes away from the root.
Algorithm
The algorithm uses recursion to calculate the maximum height:
- Recursively calculate the height of the tree to the left of the root.
- Recursively calculate the height of the tree to the right of the root.
- Pick the larger height from the two answers and add one to it (to account for the root node).
Code
#include <iostream>using namespace std;struct Node {int value;Node *left, *right;Node(int val){this->value = val;this->left = this->right = NULL;}};int maxDepth(Node * root){// Root being null means tree doesn't exist.if (root == NULL)return 0;// Get the depth of the left and right subtree// using recursion.int leftDepth = maxDepth(root->left);int rightDepth = maxDepth(root->right);// Choose the larger one and add the root to it.if (leftDepth > rightDepth)return leftDepth + 1;elsereturn rightDepth + 1;}// driver codeint main() {Node* root = new Node(1);root->left = new Node(2);root->right = new Node(3);root->left->left = new Node(4);root->right->left = new Node(5);root->right->right = new Node(6);root->right->right->left = new Node(8);root->right->left->right = new Node(7);cout << "The maximum depth is: " << maxDepth(root) << endl;}
Relevant Answers
Explore Courses
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved