求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;
        int max = 0;
        for(Node node : root.children){ //深度优先
            int count = maxDepth(node);
            if (max < count) max = count;
        }
        return max + 1;
    }
原文地址:https://www.cnblogs.com/Mark-blog/p/12918108.html