문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
n진 트리가 주어졌을 때, 최대 깊이를 구해라.
최대 깊이는 루트 노드에서 가장 먼 리프 노드까지의 가장 긴 경로에 있는 노드의 개수이다.
#1
Input: root = [1, null, 3, 2, 4, null, 5, 6]
Output: 3
#2
Input: root = [1, null, 2, 3, 4, 5, null, null, 6, 7, null, 8, null, 9, 10, null, null, 11, null, 12, null, 13, null, null, 14]
Output: 5
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public int maxDepth(Node root) {
if(root == null) return 0;
if(root.children.size() == 0) return 1;
int depth = Integer.MIN_VALUE;
for(Node node : root.children){
depth = Math.max(depth, maxDepth(node) + 1);
}
return depth;
}
}