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 ...
随机推荐
- 微信小程序组件modal
操作反馈modal:官方文档 Demo Code: Page({ data: { modalHidden: true, modalHidden2: true }, modalTap: function ...
- fzu1901Period II
地址:http://acm.fzu.edu.cn/problem.php?pid=1901 题目: Problem 1901 Period II Accept: 442 Submit: 1099 ...
- Salesforce视图与控制器之间的交互
刚接触Salesforce,过程的确是比较艰难了,中文资料几乎没有,看英文资料学的效率却不高,不过看了一段时间的英文资料发现自己英语水平挺高不少啊,现在看都不用工具翻译,早知道就再次尝试报个6级,看下 ...
- Spring AOP (事务管理)
一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...
- 使用sql语句创建 表空间 和用户,以及如何彻底删除表空间和用户,使用命令导出和导出数据库
创建表空间有很多种方式,在安装好oracle 11g 后在网站上 https://localhost:1158/em 手动创建也可以,但是没有sql直接生成方便,下面介绍下如何用sql语句直接生成表空 ...
- 打印std::tuple的N总方式
方式一:递归 + 类模板特化方式 template<typename Tuple, std::size_t N> struct tuple_printer { static void pr ...
- [转]关于Navicat和MYSQL字符集不统一出现的中文乱码问题
原文链接:关于Navicat和MYSQL字符集不统一出现的中文乱码问题 最近遇到一串关于MYSQL中文乱码的问题,问题背景是这样的: 在此之前,服务器上安装好MySQL之后就立马重新配置了字符集为ut ...
- [转]eclipse 配置黑色主题 Luna 方式三
虽然以前也使用eclipse的黑色主题,但是配置起来稍微麻烦一点. 这里先声明,下面的方式适合最新版本的Eclipse Luna,旧的版本可以下载我提供的这个插件,并将其放在eclipse目录下的 ...
- The mind
Youtube励志红人Mateusz M,最近刚推出了网友期待已久最新励志短片<The mind>,短短一周播放量已近50万.来自波兰的他,年仅23岁,用自己擅长的蒙太奇,已创作十几个影响 ...
- nginx和php之间是怎样通信的呢?
FastCGI原理 FastCGI是一个运用于Http Server和动态脚本语言间通信的接口,多数流行的Http Server都支持FastCGI,包括Apache.Nginx和lighttpd等. ...