剑指 Offer 55 - I. 二叉树的深度 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr) return 0;
return dfs(root);
}
int dfs(TreeNode*root){
if(root==nullptr) return -1;
if(root->left==nullptr&&root->right==nullptr) return 1;
int l=dfs(root->left);
int r=dfs(root->right);
return max(l,r)+1;
}
};