题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/

n-ary-tree的数据结果表示

// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};

  DFS算法:迭代

int maxDepth(Node* root) {
if (root == NULL) return ;
int max = , n = root->children.size();
for (int i = ; i<n; i++)
{
int temp=maxDepth(root->children[i]);
if (temp>max)
max = temp;
//max=std::max(max,maxDepth(root->children[i]));
}
return max + ;
}

BFS算法:队列

int maxDepth(Node* root) {
if (root == NULL) return ;
int res=;
queue<Node*> Q;
Q.push(root);
while(!Q.empty())
{
res++;
int n=Q.size();
for(int i=;i<n;i++)
{
Node* t=Q.front(); for(int j=;j<t->children.size();j++)
Q.push(t->children[j]);
Q.pop();
}
}
return res;
}

leetcode刷题-559. Maximum Depth of N-ary Tree的更多相关文章

  1. [LeetCode&Python] Problem 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 ...

  2. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  3. C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...

  4. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  5. 559. Maximum Depth of N-ary Tree - LeetCode

    Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...

  6. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  7. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  8. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. CentOS 8 (1905)系统安装

    本章内容: CentOS 8 的安装(CentOS-8-1905) 一.安装光盘,选择Install CentOS Linux 8.0.1905 二.选择系统语言,我这里选的是英文,也可以选择中文,往 ...

  2. JSONPlaceholder - 免费的在线REST服务(提供测试用的HTTP请求假数据)

    http://www.hangge.com/blog/cache/detail_2020.html 一.GET 方式请求数据1,获取文章(贴子)数据 二.POST 方式请求数据1,功能说明我们可以将数 ...

  3. 连接mongodb服务器

    连接mongodb有几种方法 一种是使用mongodb编译时生成的客户端进行连接,就是我们之前介绍过的mongo客户端 另一种是使用各种驱动进行连接 这次使用mongo客户端进行连接,之前我们启动了一 ...

  4. 01-docker简介及安装

    什么是dockerdocker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业余项目,它基于google公司推出的go语言实现.项目后来加入了linux基金会,遵从了apac ...

  5. 号外号外!WPF界面开发者福音,DevExpress支持.NET Core 3.0!

    通过DevExpress WPF Controls,你能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案. 无论是Office办公软件的衍 ...

  6. springBoot maven项目打成jar包

    springBoot项目打包springBoot项目打包最常用且最简单的方式是用springBoot的打包plugin <plugin> <groupId>org.spring ...

  7. 用jackson的@JsonProperty注解属性名,会多出一个字段

    遇见了这个情况,我的字段定义是xVal,yVal,用的lombok的@Data注解.然后查询到了下面这偏文章,https://bbs.csdn.net/topics/392305619,里面的回答是图 ...

  8. jQuery.map(arr|obj,callback)

    jQuery.map(arr|obj,callback) 概述 将一个数组中的元素转换到另一个数组中.广州大理石机械构件 作为参数的转换函数会为每个数组元素调用,而且会给这个转换函数传递一个表示被转换 ...

  9. 路由器配置——DHCP+DHCP中继服务配置

    一.实验目的:掌握DHCP服务基本配置及DHCP中继服务配置,实现全网互通 二.拓扑图: 三.具体步骤配置: (1)R1路由器配置: Router>enable  --进入特权模式 Router ...

  10. Gym - 102307D Do Not Try This Problem

     Do Not Try This Problem Gym - 102307D 题意:给个长度为len的字符串(len<=1e5),然后q次操作(q<=1e5),每次给出i,a,k,c,(i ...