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:

  1. The depth of the tree is at most 1000.
  2. 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的更多相关文章

  1. (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 ...

  2. 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 ...

  3. 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 ...

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

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

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

  6. 559. Maximum Depth of N-ary Tree - LeetCode

    Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...

  7. 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)

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

  8. [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 ...

  9. 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 ...

随机推荐

  1. eclipse 创建jsp报错

  2. PHP递归算法

    /** * 获取菜单 * @param number $id * @return multitype: */ public function menu($id = 0) { $menu = M ( ' ...

  3. eclipse web项目的发布路径

    java Build path是编译路径设置,主要用来设置源代码的编译路径默认是default output folder Web Deployment Assembly是eclipse中的发布路径设 ...

  4. C++实现最基本的LRUCache服务器缓存

    目录: 一.介绍: 二.数据结构: 三.主要的两个函数接口Put()和Get(): 四.C++代码实现: 后台开发必备知识,不过我不是搞这个的,只是因为很久以前就想写这些东西,事情多,拖到现在.写的过 ...

  5. Objective-C 集成农行支付接口

    部分参考代码: [NANetworkHandler POSTWithURL:APP_ABCPay par:dict isStored:NO success:^(id responseObject, B ...

  6. Java -D命令对应的代码中获取-D后面的参数 和 多个参数时-D命令的使用

    1. Java代码: public class TestDPara { public static void main(String[] args) { String flag = System.ge ...

  7. RabbitMQ单机多实例配置

    由于某些因素的限制,有时候你不得不在一台机器上去搭建一个rabbitmq集群,当然这种集群只适合自己玩玩,验证下结论,这个有点类似zookeeper的单机版.真实生成环境还是要配成多机集群的.有关怎么 ...

  8. webservice获取天气信息

    效果 1.eclipse中新建一个Java项目 2.通过命名获取天气的客户端信息 首先,打开天气网站http://ws.webxml.com.cn/WebServices/WeatherWS.asmx ...

  9. quickSort - 编程细节

    quicksort 快速排序 有几个细节比较重要 今天听到一个清华的大佬的话,他曾经是NOI, 在大一就得到了我梦寐以求的ACM金奖,他这样说, 他们在打NOI的时候,每天要求做10道题, 连续做60 ...

  10. git 提交某个内容

    git add 你所添加的文件或者文件夹 git commit git push