559. N 叉树的最大深度

  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. public int val;
  5. public List<Node> children;
  6. public Node() {}
  7. public Node(int _val) {
  8. val = _val;
  9. }
  10. public Node(int _val, List<Node> _children) {
  11. val = _val;
  12. children = _children;
  13. }
  14. };
  15. */
  16. class Solution {
  17. public int maxDepth(Node root) {
  18. if (root == null)
  19. return 0;
  20. else if (root.children.isEmpty())
  21. return 1;
  22. int maxDepth = 0;
  23. for (Node child : root.children)
  24. maxDepth = Math.max(maxDepth, maxDepth(child));
  25. return maxDepth + 1;
  26. }
  27. }