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 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 解题报告的更多相关文章
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- (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 ...
- 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 ...
- 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 ...
- [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 ...
- [leetcode] 559. Maximum Depth of N-ary Tree (easy)
原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...
- 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
随机推荐
- 怎样找回被删除的EXCEL
我使用的是腾讯管家进行文件恢复,步骤如下: (1)打开电脑管家,选择工具箱. (2)找到文件找回,点击. (3)选择恢复被删除的文件. (4)选择我们删除的文件. (5)文件还原后路径,点击“确认还原 ...
- 搭建redis集群
官方详细介绍请移步:http://www.redis.cn/topics/cluster-tutorial.html 这里总结性给出搭建步骤: 1. 至少6个节点,三主三从 2. 编译redis源码 ...
- idea 添加项目到svn
version control 改变项目为svn管理 如图 分享 share dic 添加新地址 share 剩下的 就是设置忽略目录 提交
- tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed
每次重启自己的服务tomcat都需要卡住很长时间,每次都是日志停在 Root WebApplicationContext: initialization completed in 744 ms这个地方 ...
- 法线从object space到eye space的转换((normal matrix)
对于顶点来说,从object Space转换到eye space, 使用model-view矩阵就好了.那么顶点的法线是否也可以直接使用model-view矩阵转化? 通常情况下是不行的. 如下两张图 ...
- opencv_java import org.opencv.highgui.Highgui,类中无imread方法
opencv_java import org.opencv.highgui.Highgui,提示错误 2018年01月19日 14:50:25 小码农的路程 阅读数:358 原因:1.OpenCV ...
- Eclipse+Maven创建webapp项目<二>
Eclipse+Maven创建webapp项目<二> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...
- 【转】使用Log4Net进行日志记录
首先说说为什么要进行日志记录.在一个完整的程序系统里面,日志系统是一个非常重要的功能组成部分.它可以记录下系统所产生的所有行为,并按照某种规范表达出来.我们可以使用日志系统所记录的信息为系统进行排错, ...
- 【转】Winform程序未捕获异常解决方法 EventType clr20r3 P1
from:http://blog.csdn.net/chichaodechao/article/details/8294922 在开发winform程序时,用到多线程,在服务器部署后运行,老是自动关才 ...
- Springboot学习笔记(三)-常用注入组件方式
包扫描@ComponentScan+组件标注注解(@Controller.@Service.@Repository.@Component) 包扫描不是必须的,指定包名后以指定的包名为准,比如指定包名为 ...