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.
解题思路:
跟思路一样,这个更简答。
int depth(TreeNode* root){
if(root->left == NULL && root->right == NULL) return 1;
int left = 0; int right = 0;
if(root->left) left = depth(root->left);
if(root->right) right = depth(root->right);
return std::max(left, right) + 1;
}
int maxDepth(TreeNode *root)
{
if(root == NULL) return 0;
return depth(root);
}