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

/*

// Definition for a Node.

class Node {

    public int val;

    public List<Node> children;

 

    public Node() {}

 

    public Node(int _val) {

        val = _val;

    }

 

    public Node(int _val, List<Node> _children) {

        val = _val;

        children = _children;

    }

};

*/

 

class Solution {

    public int maxDepth(Node root) {

        if(root==null) return 0;

        if(root.children.isEmpty()) return 1;

 

        ArrayList<Integer> list=new ArrayList<>();

        for(Node i:root.children){

            list.add(maxDepth(i));

        }

        return Collections.max(list)+1;

    }

}

相关文章: