Created
August 6, 2019 09:29
-
-
Save ajinkyajawale14499/dd64d88a36c2931ded39493c5498bd2c to your computer and use it in GitHub Desktop.
max depth of binary tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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