Update 0104.二叉树的最大深度.md

104二叉树最大深度贡献java版本的从深度角度的递归法
This commit is contained in:
zihao Tan 2022-09-29 09:41:25 +08:00 committed by GitHub
parent fd820f6f4f
commit de017e9544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -313,6 +313,31 @@ class solution {
}
```
```java
class Solution {
/**
* 递归法(求深度法)
*/
//定义最大深度
int maxnum = 0;
public int maxDepth(TreeNode root) {
ans(root,0);
return maxnum;
}
//递归求解最大深度
void ans(TreeNode tr,int tmp){
if(tr==null) return;
tmp++;
maxnum = maxnum<tmp?tmp:maxnum;
ans(tr.left,tmp);
ans(tr.right,tmp);
tmp--;
}
}
```
```java
class solution {
/**