C#LeetCode刷题之#559-N叉树的最大深度(Maximum Depth of N-ary Tree)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4088 访问。
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
例如,给定一个 3叉树 :
我们应返回其最大深度,3。
说明:
- 树的深度不会超过 1000。
- 树的节点总不会超过 5000。
Given a n-ary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
For example, given a 3-ary
tree:
We should return its max depth, which is 3.
Note:
- The depth of the tree is at most
1000
. - The total number of nodes is at most
5000
.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4088 访问。
public class Program {
public static void Main(string[] args) {
var root = new Node(1,
new List<Node> {
new Node(3, new List<Node> {
new Node(5, new List<Node>()),
new Node(6, new List<Node>())
}),
new Node(2, new List<Node>()),
new Node(4, new List<Node>())
});
var res = MaxDepth(root);
Console.WriteLine(res);
res = MaxDepth2(root);
Console.WriteLine(res);
Console.ReadKey();
}
public static int MaxDepth(Node root) {
if(root == null) return 0;
var res = 1;
Depth(root, 1, ref res);
return res;
}
public static void Depth(Node root, int depth, ref int max) {
if(root.children == null || root.children.Count == 0) {
max = Math.Max(max, depth);
return;
}
foreach(var child in root.children) {
Depth(child, depth + 1, ref max);
}
}
public static int MaxDepth2(Node root) {
if(root == null) return 0;
var res = 0;
if(root.children != null && root.children.Count != 0) {
foreach(var child in root.children) {
var depth = MaxDepth2(child);
res = Math.Max(res, depth);
}
}
return res + 1;
}
public class Node {
public int val;
public IList<Node> children;
public Node() { }
public Node(int _val, IList<Node> _children) {
val = _val;
children = _children;
}
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4088 访问。
3
3
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#559-N叉树的最大深度(Maximum Depth of N-ary Tree)的更多相关文章
- C#LeetCode刷题之#53-最大子序和(Maximum Subarray)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4012 访问. 给定一个整数数组 nums ,找到一个具有最大和的 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- C#LeetCode刷题-广度优先搜索
广度优先搜索篇 # 题名 刷题 通过率 难度 101 对称二叉树 42.1% 简单 102 二叉树的层次遍历 49.7% 中等 103 二叉树的锯齿形层次遍历 43.0% 中等 107 二 ...
- C#LeetCode刷题-深度优先搜索
深度优先搜索篇 # 题名 刷题 通过率 难度 98 验证二叉搜索树 22.2% 中等 99 恢复二叉搜索树 45.1% 困难 100 相同的树 48.1% 简单 101 对称二叉树 4 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
随机推荐
- 【软件测试】Python自动化软件测试算是程序员吗?
今天早上一觉醒来,突然萌生一个念头,[软件测试]软件测试算是程序员吗?左思右想,总感觉哪里不对.做了这么久的软件测试,还真没深究过这个问题. 基于,内事问百度的准则: 结果…… 我刚发 ...
- selenium自动化测试实战——12306铁路官网范例
一.Selenium介绍 Selenium 是什么?一句话,自动化测试工具.它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Se ...
- 瀑布流的实现纯CSS实现Jquery实现
瀑布流的实现 注:本文部分图片自百度下载,如有侵权,联系删图. 首先,选择几张图片布局到HTML内容中.HTML如下所示. <div class="wrapper"> ...
- GitHub和码云gitee及远程仓库管理
目录 备注: 知识点 GitHub 码云(gitee.com) gitee的使用 本地版本库关联多个远程库 备注: 本文参考于廖雪峰老师的博客Git教程.依照其博客进行学习和记录,感谢其无私分享,也欢 ...
- 题解 洛谷 P6351 【[PA2011]Hard Choice】
删边操作不好处理,所以先将操作倒序,将删边转化为加边. 考虑对于两个点的询问,若这两点不连通或这两个点分别处于两个不同的边双连通分量中(两点间存在桥)时,是不满足题目要求的. 可以用\(LCT\)来维 ...
- 把若依管理系统部署到Linux
一.前言 1.非常感谢若依作者为大家提供的非常优质的开源web项目,非常感谢!!! 2.若依官方文档:http://doc.ruoyi.vip/ruoyi/ 3.若依官方链接: 1)若依管理系统官方体 ...
- spring学习(四)使用注解代替xml配置
用的是IDEA的maven工程,pom.xml文件导包依赖省略 一.书写要导入容器的实体类 import org.springframework.beans.factory.annotation.Va ...
- Python之filter、map、reduce函数
简介三函数: 高阶函数:一个函数可以接收另一个函数作为参数,这种函数称之为高阶函数. filter.map.reduce三个函数都是高阶函数,且语法都一致:filter/map/reduce(func ...
- Springboot 多数据源配置,结合tk-mybatis
一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...
- Python日期和时间_什么是Tick_什么是时间元组_获取当前时间
Python 日期和时间_什么是 Tick _什么是时间元组: 时间和日期:某年某月某日某时某分某秒 Tick: 时间间隔以 秒 为单位的浮点小数,起始时间为:1970年1月1日0点0分开始 # Ti ...