/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if(root==NULL)return 0;
int max =0;
for(int i=0;i<root->children.size();i++)
{
int tmp = maxDepth(root->children[i]);
if(tmp>max)
max = tmp;
}
return max+1;
}
};
本文介绍了一种计算N叉树最大深度的算法实现。通过递归方式遍历树的每一个节点,比较并记录下每一个子树的最大深度,最终返回整棵树的最大深度。此算法适用于各种N叉树结构,为理解和实现树的深度提供了清晰的思路。
456

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



