Google+ Followers

Tuesday, December 25, 2012

[LeetCode] Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
» Solve this problem


1:    int maxDepth(TreeNode *root) {  
2:      // Start typing your C/C++ solution below  
3:      // DO NOT write int main() function  
4:      if(root == NULL)  
5:        return 0;  
6:      int lmax = maxDepth(root->left);  
7:      int rmax = maxDepth(root->right);  
8:      return max(lmax, rmax)+1;  
9:    }  

Update: This can also be implemented as iterative travel.

Post a Comment