Problem Statement :
Given a binary tree find the max depth of Binary Tree.
Solution :
Height of binary tree defined as max path length from root to leaf node. if root is NULL then height is defined as zero. Here we can apply recursive approach as maximum of height of left subtree and right subtree with +1 value of root node .
1 2 3 4 5 6 7 8 9 10 11 |
int max(int a,int b){ if(a>b) return a; else return b; } int maxdepth(struct Tnode *Troot){ if(Troot==NULL) return 0; else { return max(maxdepth(Troot->left),maxdepth(Troot->right))+1; } } |
Click here to get binary search tree implementation code.