Merge pull request #1667 from Tcotyledons/master

Update 0104.二叉树的最大深度.md
This commit is contained in:
程序员Carl
2022-10-01 16:08:29 +08:00
committed by GitHub

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 {
/**