Skip to content

Instantly share code, notes, and snippets.

@ajinkyajawale14499
Created August 6, 2019 09:29
Show Gist options
  • Select an option

  • Save ajinkyajawale14499/dd64d88a36c2931ded39493c5498bd2c to your computer and use it in GitHub Desktop.

Select an option

Save ajinkyajawale14499/dd64d88a36c2931ded39493c5498bd2c to your computer and use it in GitHub Desktop.
max depth of binary tree
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class node{
public: int data;
node* left;
node* right;
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
int maxdepth(node* node){
if(node==NULL)return 0;
else{
int ldepth=maxdepth(node->left);
int rdepth=maxdepth(node->right);
if(ldepth>rdepth) return(ldepth+1);
else return(rdepth+1);
}
}
int main()
{
node* root = new node(4);
root->left = new node(2);
root->right = new node(5);
root->left->right = new node(1);
root->left->right=new node(3);
cout<<"max depth of tree is"<<maxdepth(root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment