题目:
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.
我的解法:(1)算法思想:
递归,对于一个结点,它的左、右子树depth的最大值加上1,就得到这个结点的depth。
(2)代码如下:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root==NULL)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};