问题描述:
Given a n-ary tree, find its maximum depth.
求多叉树的最大深度
思路:
求深度的问题一般用递归: 某一个节点处的深度=其所有子节点的最大深度+1
代码如下:
class Solution {
public int maxDepth(Node root) {
//base case: if the root is null, the tree doesn't exist -> its height is 0
if(root==null) return 0;
int max=0;
for(Node node: root.children){
int cur=maxDepth(node);
if(cur>max){
max=cur;
}
}
return max+1;
}
}