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.

12/12/2019 Update: Code 可以用更简介的方式, 2 lines。

05/11/2020 Update: 利用map。

这个题目就是用DFS recursive啦.

Code:

class Solution:
def maxDepthNarry(self, root):
if not root: return 0
temp = []
for each in root.children:
temp.append(self.maxDepthNarry(each))
return 1 + max(temp + [0])

Code2:

class Solution:
def maxDepth(self, root):
if not root: return 0
return 1 + max([self.maxDepth(child) for child in children] + [0]) # + [0] because children might be [], then max([]) will throw error.

Code3:

class Solution:
def maxDepth(self, root):
if not root: return 0
children = map(self.maxDepth, root.children)
return 1 + max(children + [0]) #note: max([]) will throw error.

[LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS的更多相关文章

  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

    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(N-Tree的深度)

    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] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

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

随机推荐

  1. shell技巧之以逆序形式打印行

    测试文本内容如下: # cat textfile hadoop hdfs yarn spark zookeeper mapreduce hive hbase scala kafka CHAVIN my ...

  2. Sleep 等待连接攻击

    Sleep The thread is waiting for the client to send a new statement to it. https://dev.mysql.com/doc/ ...

  3. Java的四种内部类(含代码实例)

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  4. regular expression (如何用Sed和正则表达式提取子字符串)

    echo "mv u3_yunying.war   u3_yunying.war_`date +%Y%m%d%H%M`" | sed "s/\(mv\) \(\w*.wa ...

  5. [Day4]Switch语句、数组、二维数组

    1.选择结构switch (1)格式 switch (表达式){ case 目标值1: 执行语句1; break; case 目标值2: 执行语句2; break; ...... case 目标值n: ...

  6. DBCHART读取X、Y、LABEL值

    1.把MOUSE移到某一条SERIES上时,显示该点的值 procedure Tzl1Form.DBChart1ClickSeries(Sender: TCustomChart;  Series: T ...

  7. DBCHART直方图顶端显示数字

    双击DBCHART-->选SERIES选项卡-->选MARKS-->选STYLE值为VALUE

  8. Java问题汇总

    一.java编译通过,为什么运行却提示找不到或无法加载主类 使用运行窗口编译通过,但运行提示 通过eclipse可以运行 原因:代码中有package javaLearn:编译时在javaLearn目 ...

  9. idea找不到import project

    一.首先File->close project 关完后,在界面你就可以看到import project

  10. 经典影响力传播模型LT模型、IC模型

    一.LT模型:线性阈值模型 思想:当一个已经激活的节点去试图激活邻居节点而没有成功时,其对邻居节点的影响力被累积而不是被舍弃,这个贡献直到节点被激活或传播过程结束为止.该过程称为‘影响累积’. 过程: ...