问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. C#LeetCode刷题之#53-最大子序和(Maximum Subarray)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4012 访问. 给定一个整数数组 nums ,找到一个具有最大和的 ...

  2. C#LeetCode刷题-树

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

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

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

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

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

  5. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  6. C#LeetCode刷题-数组

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

  7. C#LeetCode刷题-广度优先搜索

    广度优先搜索篇 # 题名 刷题 通过率 难度 101 对称二叉树   42.1% 简单 102 二叉树的层次遍历   49.7% 中等 103 二叉树的锯齿形层次遍历   43.0% 中等 107 二 ...

  8. C#LeetCode刷题-深度优先搜索

    深度优先搜索篇 # 题名 刷题 通过率 难度 98 验证二叉搜索树   22.2% 中等 99 恢复二叉搜索树   45.1% 困难 100 相同的树   48.1% 简单 101 对称二叉树   4 ...

  9. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  10. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

随机推荐

  1. 【Nginx】如何为已安装的Nginx动态添加模块?看完我懂了!!

    写在前面 很多时候,我们根据当时的项目情况和业务需求安装完Nginx后,后续随着业务的发展,往往会给安装好的Nginx添加其他的功能模块.在为Nginx添加功能模块时,要求Nginx不停机.这就涉及到 ...

  2. React js ReactDOM.render 语句后面不能加分号

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  3. java中同步异步阻塞和非阻塞的区别

    同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回. 按照这个定义,其实绝大多数函数都是同步调用(例如sin, isdigit等).但是一般而言,我们在说同步.异步的时候,特 ...

  4. 基于python的自动化测试框架搭建

    滴~ 今日打卡!   好多天没来打卡了.博主最近一直在把碎片化知识转化为知识体系的过程中挣扎.Python语言.selenium.unittest框架.HTMLTestRunner框架都有所了解,也写 ...

  5. CocosCreator之分层管理的ListView

    前言 进入公众号回复listview即可获得demo的git地址. 之前写的一篇文章<Creator之ScrollView那些事>中提到了官方Demo中提供的ListViewCtl,只是实 ...

  6. P2070 刷墙 (洛谷)

    题目描述 Farmer John已经设计了一种方法来装饰谷仓旁边的长栅栏(把栅栏认为是一根一维的线).他把一只画刷绑在他最喜爱的奶牛Bessie身上,之后就去喝一杯冰水,而Bessie隔着栅栏来回走, ...

  7. cmd : 代理设置/检验代理设置成功

    设置代理很简单,一句话的事儿. set HTTP_PROXY=http://user:password@proxy.domain.com:port 比如说,我用ssr,默认地址是127.0.0.1:1 ...

  8. 阿里云的maven仓库

    自从开源中国的maven仓库挂了之后就一直在用国外的仓库,慢得想要砸电脑的心都有了.如果你和我一样受够了国外maven仓库的龟速下载?快试试阿里云提供的maven仓库,从此不在浪费生命…… 仓库地址: ...

  9. springboot(七)JavaMail发送邮件

    JavaMail简介: JavaMail是SUN提供给广大Java开发人员的一款邮件发送和接受的一款开源类库,支持常用的邮件协议,如:SMTP.POP3.IMAP,开发人员使用JavaMail编写邮件 ...

  10. 服务器入侵应急响应排查(Linux篇)

    总体思路 确认问题与系统现象 → 取证清除与影响评估 → 系统加固 → 复盘整改 常见入侵 ① 挖矿: 表象:CPU增高.可疑定时任务.外联矿池IP. 告警:威胁情报(主要).Hids.蜜罐(挖矿扩散 ...