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 ...
随机推荐
- linux grep 取出特定字符串并统计个数
原始日志如下: $more text.log 2018-07-16 00:00:03 [DEBUG] request setInformation params:{"msg":&q ...
- 业界常用的和不常用cad快捷键
AutoCAD 是目前世界各国工程设计人员的首选设计软件,简便易学.精确无误是AutoCAD成功的两个重要原因.AutoCAD提供的命令有很多,绘图时最常用的命令只有其中的百分之二十. 在CAD软件操 ...
- Git 移动操作
顾名思义移动(move )操作移动目录或文件从一个位置到另一个.Tom 决定移动到src目录下的源代码.因此,修改后的目录结构看起来会像这样. [tom@CentOS project]$ pwd /h ...
- Mysql 地区经纬度 查询
摘要: Mysql数据库,根据地区的经纬度信息,查询附近相邻的地区 2015-03-27 修改,增加 MySQL的空间扩展(MySQL Spatial Extensions)的解决方案: MySQL的 ...
- JavaScript高级用法三之浏览器对象
综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 window对象 2 JavaScript 计时器 3 计时器setInterval() 4 取消计时器clearInterval() 5 ...
- RDP 数据库简介
在扩增子数据分析中,有时会发现多个OTU 注释到了同一个species , 为什么会出现这种情况呢? 首先既然在OTU水平能分开,说明序列的相似度小于97%, 同一个物种的同一个基因的片段相似度会 ...
- Spark学习笔记——读写HDFS
使用Spark读写HDFS中的parquet文件 文件夹中的parquet文件 build.sbt文件 name := "spark-hbase" version := " ...
- Scala学习笔记(三):==,eq与equals的区别
== Scala中==与java中不同,它是比较值是否相等的,无论比较对象是否是相同类型 List(1, 2, 3) == List(1, 2, 3) //true 1==1.0//true equa ...
- python 切片获取list、tuple中的元素
#-*- coding:UTF-8 -*- L=[] n=6 r=[1,2,3,4,5,6] for i in range(n): L.append(r[i]) print L # =>[1, ...
- Python 函数(默认参数)
默认参数 设置默认参数时,有两点需要注意:一是必选参数在前,默认参数在后,否则python的解释器会报错二是当函数有多个参数时,把变化大的参数放前面,变化小的放后面,变化小的参数就可以作为默认参数 d ...