题目
类型:深度优先搜索
解题思路
从 root 的所有子节点中的取最大深度,并在此基础上加一(统计 root 节点)即是答案。
代码
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int ans = 0;
for (Node node : root.children) {
ans = Math.max(ans, maxDepth(node));
}
return ans + 1;
}
}