题目要求

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.

题目分析及思路

题目给出一个N叉树,要求得到它的最大深度。该最大深度为根结点到最远叶结点的结点数。可以使用递归,遍历孩子结点。

python代码​

"""

# Definition for a Node.

class Node:

def __init__(self, val, children):

self.val = val

self.children = children

"""

class Solution:

def maxDepth(self, root):

"""

:type root: Node

:rtype: int

"""

if not root:

return 0

elif not root.children:

return 1

else:

c = []

for child in root.children:

c.append(self.maxDepth(child))

c.sort()

return 1 + c[-1]

LeetCode 559 Maximum Depth of N-ary Tree 解题报告的更多相关文章

  1. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. (N叉树 DFS 递归 BFS) leetcode 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 ...

  4. leetcode 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 ...

  5. LeetCode 559. Maximum Depth of N-ary Tree(N-Tree的深度)

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  6. [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  7. [leetcode] 559. Maximum Depth of N-ary Tree (easy)

    原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...

  8. 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  9. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

随机推荐

  1. javascript form提交 不执行onsubmit事件解决方案

    转载自:https://www.cnblogs.com/lorgine/archive/2011/03/30/2000284.html 今天做项目过程中,需要用到javascript提交form到后台 ...

  2. 【iCore4 双核心板_ARM】例程八:定时器PWM实验——呼吸灯

    实验原理: STM32的定时器有PWM功能,iCore4的蓝色LED连接在定时器的输出接口上, 可以通过定时器的PWM输出控制LED的亮度,从而实验呼吸灯的功能. 核心代码: int main(voi ...

  3. 【iCore1S 双核心板_FPGA】例程十六:基于SPI的ARM与FPGA通信实验

    实验现象: 核心代码: int main(void) { int i,n; ]; ]; HAL_Init(); system_clock.initialize(); led.initialize(); ...

  4. 用opencv抽取视频的帧并保存为连续的图片

    转自http://blog.csdn.net/timidsmile/article/details/8283319 #include"stdafx.h" #include < ...

  5. 爱快路由计费系统easyradius隆重发布,支持V2版本,欢迎大家测试使用

    随着PA.BV接口的发布的临近,我们已经对ROS及爱快V2接口进行大量的升级工作 经用户测试,各方便已满足用户需求. 其实很早以前我们就有支持爱快路由的打算,但是由于各方便原因,通讯接口做好了,但是并 ...

  6. git解决 remote: Permission to wuheng1991/site-manager.git denied to XXX

    1.问题 2.解决 生成一个新的SSH KEY ssh-keygen  -t rsa –C “youremail@example.com” 命令: 3.修改 .git/config中的url 4.gi ...

  7. centos7设置时间和时区

    1.安装ntp服务软件包:yum install ntp 2.将ntp设置为缺省启动:systemctl enable ntpd 3.修改启动参数,增加-g -x参数,允许ntp服务在系统时间误差较大 ...

  8. <aop:aspectj-autoproxy />作用

    通过配置织入@Aspectj切面 虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作. 通过aop命名空间的<aop:aspectj-a ...

  9. MySql表结构修改详解

    修改表的语法=========================增加列[add 列名]=========================①alter table 表名 add 列名 列类型 列参数[加的 ...

  10. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二:按键模块① - 消抖

    实验二:按键模块① - 消抖 按键消抖实验可谓是经典中的经典,按键消抖实验虽曾在<建模篇>出现过,而且还惹来一堆麻烦.事实上,笔者这是在刁难各位同学,好让对方的惯性思维短路一下,但是惨遭口 ...