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.
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
.
DFS:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root: return 0
if not root.children: return 1
depths = [self.maxDepth(node) for node in root.children]
return max(depths)+1
简化下,
class Solution(object):
def maxDepth(self, root):
if not root: return 0
if not root.children: return 1
return max(self.maxDepth(node) for node in root.children) + 1
或者是:
class Solution(object):
def maxDepth(self, root):
return 1 + max([self.maxDepth(n) for n in root.children] + [0]) if root else 0
BFS:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root: return 0
ans = 0
q = [root]
while q:
ans += 1
q = [c for node in q for c in node.children if c]
return ans
leetcode 559. Maximum Depth of N-ary Tree的更多相关文章
- (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 ...
- 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) ...
- 559. Maximum Depth of N-ary Tree - LeetCode
Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...
- 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [LeetCode&Python] Problem 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 104. Maximum Depth of Binary Tree (二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- struts.xml 配置文件的主要元素
1.package元素 作用: 在struts2的配置文件中引入了面向对象思想.分包管理,易于管理动作类,便于模块化开发动作类. 属性: name:包的名称.名称唯一. extends:一般情况下需要 ...
- quartz (二) Spring+Quartz实现定时任务的配置方法
JobDetail 设置执行的任务 :CronTrigger 触发器:设置执行的时间规则 ; Scheduler // 调度器,将任务与执行时间关联 本文转自:http://w ...
- iOS开发之UITableViewController重写
# UITablViewController方法的使用介绍 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // ...
- No module named _tkinter
https://www.douban.com/note/524197380/?type=like
- MysQL使用一高级应用(下)
连接查询 连接查询分类如下: 表A inner join 表B:表A与表B匹配的行会出现在结果中 表A left join 表B:表A与表B匹配的行会出现在结果中,外加表A中独有的数据,未对应的数据使 ...
- java中使用Ehcache缓存数据
知识点:在java项目中,使用ehcache缓存数据 参考博客:http://www.cnblogs.com/jingmoxukong/p/5975994.html ()概述 Ehcache是一个纯J ...
- C# 随机数生成避免重复
public string GetMsgID() { Random rand = new Random((int)DateTime.Now.Ticks); string szRand = rand.N ...
- Ice Cream Tower
2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output f ...
- Java类继承关系中的初始化顺序
Java类初始化的顺序经常让人犯迷糊,现在本文尝试着从JVM的角度,对Java非继承和继承关系中类的初始化顺序进行试验,尝试给出JVM角度的解释. 非继承关系中的初始化顺序 对于非继承关系,主类Ini ...
- go入门环境配置
1.安装golang(64位).MinGW(64位).LiteIDE(32位) 下载golang安装包,双击安装,默认安装目录:C:\Go: MinGW安装包(x86_64-4.8.2-release ...