N叉树的最大深度
程序员文章站
2022-03-24 16:06:38
...
package T;
import java.util.List;
class NTreeNode{
int val ;
List<NTreeNode> children;
public NTreeNode(){
}
public NTreeNode(int val,List<NTreeNode> children) {
this.val = val;
this.children = children;
}
}
public class T27_559A {
//n叉树的最大深度
public static int maxDepth(NTreeNode root) {
if(root == null) {
return 0;
}
int max =0;
for(int i=0;i<root.children.size();i++) {
max = Math.max(maxDepth(root.children.get(i)), max);
}
return max+1;
}
}