题解 | #二叉树的最大深度#
二叉树的最大深度
https://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73
package com.hhdd;
import com.hhdd.数据结构.树.TreeNode;
/**
* @Author huanghedidi
* @Date 2024/3/20 21:16
*/
public class 二叉树的最大深度 {
static int res = 0;
public static int maxDepth(TreeNode root) {
int curDepth = 0;
traverse(root, curDepth);
return res;
// write code here
}
public static void traverse(TreeNode node, int curDepth) {
if (node == null) {
return;
}
curDepth++;
if (curDepth > res) {
res = curDepth;
}
traverse(node.left, curDepth);
traverse(node.right, curDepth);
}
}