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.
题目大意:求二叉树的最大深度。
解题思路:没啥好说的,直接DFS求解。
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root) {
if(root == NULL) return 0;
int left = 1 + maxDepth(root->left);
int right = 1 + maxDepth(root->right);
return left >= right ? left : right;
}