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. inline-block布局错位问题

    如图, 两个display为 inline-block的元素,会出现情况 针对第三种情况: 需要添加 vertical-align: top; 参考代码 <!DOCTYPE html> & ...

  2. [C语言]小记q = (++j) + (++j) + (++j)的值

    根据不同的编译器,生产的代码不一样,导致的结果也会不一样. 代码如下: #include <stdio.h> void main() { ; int q; q =(++j)+(++j)+( ...

  3. .net操作xml文件(新增.修改,删除,读取) 转

    今天有个需求需要操作xml节点.突然见遗忘了许多.上网看了些资料.才整出来.脑袋真不够用.在这里把我找到的资料共享一下.方便以后使用.本文属于网摘/ 1 一.简单介绍2 using System.Xm ...

  4. PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

    题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...

  5. Mysql之正则匹配

    Regex与Like的关系Mysql中我们经常会用到正则表达式就是Like filed like '%?%' .但是有时对于一些复杂场景下的正则过滤,单单一个like就显得有些力不从心了 Regex的 ...

  6. Codeforces Round #425 (Div. 2) C - Strange Radiation

    地址:http://codeforces.com/contest/832/problem/C 题目: C. Strange Radiation time limit per test 3 second ...

  7. 牛客国庆集训派对Day3 I. - Metropolis (Dijkstra变型)

    题意:求一个N个点无向图中,其中p个关键点间的最短距离. 分析:比较特殊的最短路,方式类似于多源BFS,将所有关键点装入优先队列,状态中需要包含其源点的id.对每条边都要遍历,对每个节点,需要记录其确 ...

  8. Winter-1-B Sum 解题报告及测试数据

    Time Limit:500MS Memory Limit:32768KB Description ​Hey, welcome to HDOJ(Hangzhou Dianzi University O ...

  9. 设置HTML5的video播放速度

    var speed = 1.5;//设置速度 var vdo = document.getElementById("视频的id");//获取id vdo.playbackRate ...

  10. jQuery中this 和 $(this)

    var node = $('#id'); node.click(function(){ this.css('display','block'); //报错  this是一个html元素,不是jquer ...