LeetCode 559. Maximum Depth of N-ary Tree
using recursion to solve this problem:
maxDepth(root) = Math.max(maxDepth(root.kid1), Math.max(root.kid2)…) + 1;
class Solution {
public int maxDepth(Node root) {
if (root == null) return 0;
int height = 0;
for (Node child: root.children) {
height = Math.max(height, maxDepth(child));
}
return height + 1;
}
}

浙公网安备 33010602011771号