问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Active Directory - Creating users via PowerShell

    Method1: Create a user by executing the following PowerShell Script. New-ADUser -name 'Michael Jorda ...

  2. Python Ethical Hacking - Malware Analysis(1)

    WRITING MALWARE Download file. Execute Code. Send Report. Download & Execute. Execute & Repo ...

  3. IE11 CSS hack

    IE11 怎么做 CSS hack ? 很简单. @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { ...

  4. flask下直接展示mysql数据库 字段

    在工作中,会导出一份mysql的html来查看,用的是就是路过秋天大神的那个工具,所以想自己用那个样式直接在后端写一个页面做展示! 前端页面 from flask import Flask,reque ...

  5. 浅谈CSRF(Cross-site request forgery)跨站请求伪造

    本文目录 CSRF是什么 CSRF攻击原理 CSRF攻击防范 CSRF是什么 CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack” ...

  6. 数据库事务的四个特性ACID

    原子性[Atomicity] 原子性指的指的就是这个操作,要么全部成功,要么全部失败回滚.不存在其他的情况. 一致性(Consistency) 一致性是指事务必须使数据库从一个一致性状态变换到另一个一 ...

  7. 让表单input等文本框为只读不可编辑的方法-转

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...

  8. ken桑带你读源码 之scrapy

    开篇声明 文章讲解源码不一定从入口开始   主题更注重 思路讲解以及核心函数   ok?  废话到此为止 /scrapy/downloadermiddlewares/  文件夹下是下载器的 中间件  ...

  9. Kylin Flink Cube 引擎的前世今生

    Apache Kylin™ 是一个开源的.分布式的分析型数据仓库,提供 Hadoop/Spark 之上的 SQL 查询接口及多维分析(OLAP)能力以支持超大规模数据,它能在亚秒内查询巨大的表. Ky ...

  10. Python os.remove() 方法

    概述 os.remove() 方法用于删除指定路径的文件.如果指定的路径是一个目录,将抛出OSError.高佣联盟 www.cgewang.com 在Unix, Windows中有效 语法 remov ...