LeetCode-----编号559

DFS遍历整课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 {
//记录最大深度
int Depth;
public int maxDepth(Node root) {
if(root == null)return 0;
max_D(root,1);
return Depth;
}
public int max_D(Node root, int Max){
if(root == null) return Max ;
if(root.children.size() == 0){
//一旦到达叶子结点就与上一次到达叶子结点时的深度比较大小,以此类推得出最大深度
Depth = Math.max(Depth,Max);
}
for(int i = 0 ; i < root.children.size() ; i++){
max_D(root.children.get(i),Max+1);
}
return Depth;
}
}
1021

被折叠的 条评论
为什么被折叠?



