给定一个 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. <爬虫>黑板爬虫闯关02

    import requests from lxml import etree ''' 黑板爬虫闯关02 网址:http://www.heibanke.com/lesson/crawler_ex01/ ...

  2. 大型SQL文件导入mysql方案

    一. 场景 现有俩个体积较大的单表sql文件,一个为8G,一个为4G,要在一天内完整导入到阿里云的mysql中,需要同时蛮子时间和空间这俩种要求. 二. 思路 搜索了网上一堆的方案,总结了如下几个: ...

  3. 跳过爱奇艺优酷vip

      1.google chrome浏览器  2.下载插件安装 Tampermonkey  https://pan.baidu.com/s/1qvRQD2UO6gPHogjtSUBwUw       将 ...

  4. Android开发 获取View的尺寸的2个方法

    前言 总所周知,在activity启动的onCreate或者其他生命周期里去获取View的尺寸是错误的,因为很有可能View并没有初始化测量绘制完成.你这个时候获取的宽或的高不出意外就是0.所以,我们 ...

  5. 安装tomcat8.5

    1.去官网下载安装文件 网址:https://tomcat.apache.org/download-80.cgi 2.在安装目录的bin中运行startup.bat即可启动 3.启动好tomcat后, ...

  6. pytorch基础2

    下面是常见函数的代码例子 import torch import numpy as np print("分割线---------------------------------------- ...

  7. Spring boot配置Dubbo三种方式

    方式一 使用注解的方式 导入dubbo-starter 在application.properties配置属性 使用@Service暴露服务 使用@Reference引用服务 使用@EnableDub ...

  8. 《你不知道的javascript》上卷笔记整理(一)

    函数声明和变量声明都会被提升,但函数声明会被提升到普通变量前,而 var foo = function bar(){}; 赋值操作不会被提升. 闭包: 基于词法作用域(作用域是根据名称查找变量的一套规 ...

  9. linux ssh密钥认证, 免密码登陆

    1. 客户端生成密钥 # mkdir ~/.ssh # chmod ~/.ssh # cd ~/.ssh 生成RSA密钥 # ssh-keygen -t rsa (然后连续三次回车) 2. 把公钥传到 ...

  10. android Serializable 和 Parcelable 区别

      android 中自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable. 一 序列化原因: 1.永久性保存对象,保存对象的字节序列到本地文件中:2.通过 ...