给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

说明:

  1. 树的深度不会超过 1000。
  2. 树的节点总不会超过 5000。
class Solution {
public:
int maxDepth(Node* root) {
if(root == NULL)
return 0;
int cnt = 0;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
int s = q.size();
cnt++;
while(s--)
{
Node* node = q.front();
q.pop();
for(int i = 0; i < node ->children.size(); i++)
{
if(node ->children[i] != NULL)
{
q.push(node ->children[i]);
}
}
}
}
return cnt;
}
};

Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度的更多相关文章

  1. [LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  2. Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. LeetCode559. Maximum Depth of N-ary Tree

    第一次写出了具有迭代和递归的函数,还是有点收获的,虽然题目比较简答 当要对某些对象重复使用时,考虑循环,也就是迭代 当函数可以简化一个重复的操作时,考虑递归,而且就当下一次使用这和函数的结果已经有啦, ...

  5. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  6. (N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  7. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  8. C#LeetCode刷题之#559-N叉树的最大深度​​​​​​​(Maximum Depth of N-ary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4088 访问. 给定一个 N 叉树,找到其最大深度. 最大深度是指 ...

  9. C#LeetCode刷题之#104-二叉树的最大深度​​​​​​​(Maximum Depth of Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4072 访问. 给定一个二叉树,找出其最大深度. 二叉树的深度为根 ...

随机推荐

  1. 压测:mysqlslap

    MySQL从5.1.4版开始带有一个压力测试工具mysqlslap,通过模拟多个并发客户端访问mysql来执行测试,使用起来非常简单,通过mysqlslap –help可以获得可用的选项.这里列一些主 ...

  2. 2019个人计划与Flag与期望

    突然发现写博客是真的好. 希望未来能在其他地方写上日记. 总结2018中的个人缺陷: 1.忘掉了学习方法或者说学习方法不正确 2.偶尔就会去偷下懒,对自己不够严格,自控能力差. 3.心态虽比以前好很多 ...

  3. leetcode-第5周双周赛-1135-最低成本联通所有城市

    方法一: class Solution: def minimumCost(self, N: int, conections: List[List[int]]) -> int: def find( ...

  4. leetcode146周赛-5130-等价多米诺骨牌对的数量

    题目描述: 方法一: class Solution(object): def numEquivDominoPairs(self, dominoes): """ :type ...

  5. g2o学习资料

    g2o下载:https://github.com/RainerKuemmerle/g2o/tree/9b41a4ea5ade8e1250b9c1b279f3a9c098811b5a g2o的基本框架结 ...

  6. CF402D 【Upgrading Array】

    题目链接: CF402D 题目分析: 首先考虑一下怎么求每个数的分数.把每个数分解到最后会发现它的坏质因子对它分数的贡献是\(-1\),好质因子对它分数的贡献是\(1\),那么最后的分数就是好质因数- ...

  7. hibernate使用truncate清空表 截断表

    public void truncateTable(Session session, String tableNameInDb) { String sql = " truncate tabl ...

  8. css3之 过渡

    早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成.但在CSS3中新增加了一个新的模块transition,它可以通过一些简单的CSS事件来触发元素的外观变化,让效果显得更 ...

  9. MySQL模拟Oracle序列使用

    https://www.runoob.com/mysql/mysql-using-sequences.html   一篇笔记开始看 注意:创建序列表时一定要有 主键id自增,否则为只读状态不能修改递增 ...

  10. Diff- Linux必学的60个命令

    1.作用 diff命令用于两个文件之间的比较,并指出两者的不同,它的使用权限是所有用户. 2.格式 diff [options] 源文件 目标文件 3.[options]主要参数 -a:将所有文件当作 ...