思路

递归在树中真的很神奇,我感觉他行他就行了

image-20241118144935561

每遍历一层+1,其他的我也说不出来了…

代码:

1
2
3
4
5
6
7
8
9
10
11
12
int max(int a,int b){
return a>b?a:b;
}
int dep=0;
int maxDepth(struct TreeNode* root) {
if(root==NULL){
return 0;
}
else {
return max(maxDepth(root->left)+1,maxDepth(root->right)+1);
}
}