题目

image.pngimage.png

解题思路

递归

解题代码

  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 ) return 0;
  19. int depth = 0;
  20. for(Node node : root.children) {
  21. depth = Math.max( maxDepth(node),depth );
  22. }
  23. return depth + 1;
  24. }
  25. }