TutorialStudyMite

Height of a Binary Tree

Sstudymite1 min read
Beginner friendly

Track completion, mastery, and revision.

To calculate the height of a binary tree we'll calculate the height of left tree and height of right tree and return the max of both.

#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node(int data){
this->data = data;
left = right = NULL;
}
};
int heightOfTree(Node* root){
if(!root) return 0;

}
int main() {
/*
1
/    
2      3
/   \   /  
4     5 6    7
 root = new Node(1);
root->left = new Node(2);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right = new Node(3);
root->right->left = new Node(6);
root->right->right = new Node(7);
cout<<heightOfTree(root);
return 0;
}

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.