mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0104.二叉树的最大深度.md
104二叉树最大深度贡献java版本的从深度角度的递归法
This commit is contained in:
@ -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 {
|
||||
/**
|
||||
|
Reference in New Issue
Block a user