t题目:
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.
解法:
思路很简答,就是一个递归,然后向上返回当前的最大层,一直到根节点
class Solution {
public:
int maxDepth(TreeNode* root) {
int l = 0;
int r = 0;
if(root == NULL)
return 0;
l = maxDepth(root->left);
r = maxDepth(root->right);
if(l < r)
l = r;
return l + 1;
}
};