Leetcode 559. N叉树的最大深度

题目链接

https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/

题目描述

给定一个N叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

说明:

  1. 树的深度不会超过 1000。
  2. 树的节点总不会超过 5000。

题解

采用递归遍历节点集合,求出最大值。

代码

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
    public int maxDepth(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null ) { return 0;}
        int res = 1;
        for (Node node : root.children) {
            res = Math.max(res, maxDepth(node) + 1);
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/xiagnming/p/9542188.html