问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. redis linux开机启动 (简单高效)

    1. 在edis下载文件包中找 redis/utils 找到redis_init_script 将它拷贝到  /etc/init.d 目录并重命名为redis cd redis cd utils mv ...

  2. PHP实现多继承

    题问php是否支持多继承? 答案:不可以,只支持单继承. 如何实现多继承呢? 答案:可以使用 interface 或 trait 实现 . interface这里我们就不做过多的说明了,它的原理就是一 ...

  3. mybatis generator 的日常使用

    一.mybatis-generator的基本配置与使用 使用mybatis-generator来生成常用的dao层类与xml,可以满足基础的增删改查功能 1. 添加pom依赖,常用的几个依赖包 < ...

  4. 代码Verify简介

    序 对于开发者而言,编译代码和提交代码是必不可少的流程,同一个需求反复提交的情况也时常出现,那么怎么避免这种情况,且保证代码的质量,这就是Verify CI的目标.Verify表示认证验证的意思,结合 ...

  5. vue-过渡动画和 第三方动画库导入,带图

    vue路由过渡动画 //用transition将路由出口包裹起来 <transition name="fade" mode="out-in"> &l ...

  6. python学习之路------你想要的都在这里了

    python学习之路------你想要的都在这里了 (根据自己的学习进度后期不断更新哟!!!) 一.python基础 1.python基础--python基本知识.七大数据类型等 2.python基础 ...

  7. LQB201808全球变暖 bfs

    #include<iostream> #include<stdio.h> #include<stdlib.h> #include<time.h> #in ...

  8. LVS+Keepalived 实现高可用负载均衡

    前言 在业务量达到一定量的时候,往往单机的服务是会出现瓶颈的.此时最常见的方式就是通过负载均衡来进行横向扩展.其中我们最常用的软件就是 Nginx.通过其反向代理的能力能够轻松实现负载均衡,当有服务出 ...

  9. 「从零单排canal 06」 instance模块源码解析

    基于1.1.5-alpha版本,具体源码笔记可以参考我的github:https://github.com/saigu/JavaKnowledgeGraph/tree/master/code_read ...

  10. PHP入门之函数

    前言 之前对PHP的类型.运算符和流程控制简单说了一下.想了解的,这是地址. PHP入门之类型与运算符 PHP入门之流程控制 下面对函数简单说一下. 函数的基本概念 为完成某一个功能的程序指令的合集, ...