559. Maximum Depth of N-ary Tree 发表于 2021-11-13 DFS12345678910111213141516class Solution { public int maxDepth(Node root) { if (root == null) { return 0; } int maxDepth = 0; if (root.children != null) { for (Node childNode : root.children) { maxDepth = Math.max(maxDepth(childNode), maxDepth); } } return maxDepth + 1; }} BFS12345678910111213141516171819202122class Solution { public int maxDepth(Node root) { int depth = 0; Queue<Node> queue = new LinkedList<>(); if (root != null) { queue.add(root); } while (!queue.isEmpty()) { for (int i = queue.size(); i > 0; i--) { Node node = queue.poll(); if (node.children != null && !node.children.isEmpty()) { queue.addAll(node.children); } } depth++; } return depth; }} Reference559. Maximum Depth of N-ary Tree