原题链接
不要自卑,去提升实力
互联网行业谁技术牛谁是爹
如果文章可以带给你能量,那是最好的事!请相信自己
加油o~
559.N叉树的最大深度
解题思路:
利用深度优先遍历
max代表此时能够到达的深度最大值
depth为每次向下递归加1,表示深度+1
直到它为叶子节点为止,进行回溯
代码:
/**
*作者:魏宝航
*2020年11月25日,上午8:04
*/
class Solution {
public int max=0;
public int maxDepth(Node root) {
maxDepth(root,1);
return max;
}
public void maxDepth(Node root,int depth){
if(root==null){
return;
}
max=max>depth?max:depth;
for(Node i:root.children){
maxDepth(i,depth+1);
}
}
}
执行结果: