问题描述:
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递归求,比较左右子树谁最大:l>r?l+1:r+1
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int l,r;
l=maxDepth(root->left);
r=maxDepth(root->right);
return l>r?l+1:r+1;
}
};
public:
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int l,r;
l=maxDepth(root->left);
r=maxDepth(root->right);
return l>r?l+1:r+1;
}
};