N叉树的最大深度_学习记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37770023/article/details/82808340

【说明】

 

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

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

例如,给定一个 3叉树 :

 

【算法实现】

 

/*

// 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;

    }

};

*/

/*

* @author Guozhu Zhu

* @date 2018/9/21

* @version 1.0

*

*/

class Solution {

    public int maxDepth(Node root) {

        if (root == null) {

            return 0;

        }

        int res = 1;

        for (Node node : root.children) {

            res = Math.max(res, maxDepth(node)+1);

        }

        return res;

    }

}

 

猜你喜欢

转载自blog.csdn.net/weixin_37770023/article/details/82808340
今日推荐