给定n-ary树,找到它的最大深度。
最大深度是从根节点到最远叶节点的最长路径上的节点数。
层序遍历。
class Solution {
public int maxDepth(Node root) {
if(root == null) return 0;
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root);
int depth = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0;i < size;i++){
List<Node> curr = queue.poll().children;
for(int j = 0;j < curr.size();j++){
queue.offer(curr.get(j));
}
}
depth++;
}
return depth;
}
}