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.
[Code]
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.
http://leetcode.com/2010/04/maximum-height-of-binary-tree.html
No comments:
Post a Comment