559. Maximum Depth of N-ary Tree - LeetCode

Posted okokabcd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了559. Maximum Depth of N-ary Tree - LeetCode相关的知识,希望对你有一定的参考价值。

Question

559. Maximum Depth of N-ary Tree

技术分享图片

Solution

题目大意:N叉树求最大深度

思路:用递归做,树的深度 = 1 + 子树最大深度

Java实现:

/*
// 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) {
        if(root == null) return 0;
        int depth = 0;
        for (Node child : root.children) {
            depth = Math.max(depth, maxDepth(child));
        }
        return depth + 1;
    }
}

以上是关于559. Maximum Depth of N-ary Tree - LeetCode的主要内容,如果未能解决你的问题,请参考以下文章

559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree

[LeetCode] 559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree - LeetCode

559. Maximum Depth of N-ary Tree

LeetCode559. Maximum Depth of N-ary Tree