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. 170830、oracle密码过期ORA-28002: 7天之后口令将过期的解决方法

    登陆oracle数据库时错误信息提示:“ORA-28002: 7 天之后口令将过期”. 原因:oracle11g中默认在default概要文件中设置了"PASSWORD_LIFE_TIME= ...

  2. python金融与量化分析------Matplotlib(绘图和可视化)

    -----------------------------------------------------------Matplotlib:绘图和可视化------------------------ ...

  3. Python2.7编译失败 Failed to build these modules:_curses_panel _hashlib _ssl

    1:*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.0: cannot open sha ...

  4. zabbix监控告警Received empty response from Zabbix Agent Assuming that agent dropped connection

    zabbix监控告警Received empty response from Zabbix Agent Assuming that agent dropped connection错误 查看zabbi ...

  5. VS没办法调试,直接退出,报错:1. 使用调试生成配置或禁用调试选项“启用‘仅我的代码’”。。。

    打开一个Demo,结果没办法调试,运行出错,直接退出了, 明明加了断点的. 输出→调试→提示信息如下 . 使用调试生成配置或禁用调试选项“启用‘仅我的代码’”. . 检查调试选项下的“符号”设置.线程 ...

  6. 在VMware中使用Nat方式设置静态IP, 宿主机可以 ssh

    坑很多:  麻痹,  主要还是要先 防火墙关掉,永久关掉.  seliux 也永久关掉. 临时关闭防火墙:systemctl stop firewalld    开机不启动: systemctl di ...

  7. [No0000F1]js获取喜马拉雅和荔枝FM电台专辑音频

    荔枝FM小书签.txt javascript: (function() { if ($('#down_url')) { $('#down_url').remove(); }; $(document.b ...

  8. esxi导出ovf模板注意事项

    1.网卡配置文件注释掉MAC地址 2.编辑设置,CD/DVD选择客户端设备

  9. Apache Common Math Stat

    http://commons.apache.org/proper/commons-math/userguide/stat.html mark   DescriptiveStatistics maint ...

  10. LeetCode 429 N-ary Tree Level Order Traversal 解题报告

    题目要求 Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to r ...