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. day10 多媒体(文字 图片 音频 视频)

    1计算机表示图形的几种方式     bmp:以高质量保存     用于计算机     jpg:以良好的质量保存    用于计算机或者网络     png:以高质量保存     图片大小的计算公式:图片 ...

  2. nodejs的koa2框架

    官网文档 cnpm i --save-dev koa2 koa-router koa-body koa-static request npm install --save koa2 const koa ...

  3. Pretty Smart? Why We Equate Beauty With Truth

    Pretty Smart? Why We Equate Beauty With Truth With some regularity we hear about the latest beauty-p ...

  4. 阿里云不同账号之间相同地域的VPC网络互访

    今天实际操作了一下,在这篇随笔中记录一下以备忘,主要参考阿里云帮助文档-不同账号下专有网络内网互通. 实现场景:账号A的VPC网络中的ECS访问账号B的VPC网络中的ECS与RDS(地域都在华东1), ...

  5. [No0000109]Git2/9-安装Git

    最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...

  6. TensorRT优化过程中的dropout问题

    使用tensorRT之前,你一定要注意你的网络结构是否能够得到trt的支持,无论是CNN还是RNN都会有trt的操作. 例如:tf.nn.dropout(features, keep_prob),tr ...

  7. python中OrderedDict的使用

    很多人认为python中的字典是无序的,因为它是按照hash来存储的,但是python中有个模块collections(英文,收集.集合),里面自带了一个子类 OrderedDict,实现了对字典对象 ...

  8. ATM JAVA实现 部分代码

    package score;//信1705-2 20173600 王重阳 import java.util.Scanner; public class Main { public static voi ...

  9. CSS:盒模型和position定位

    盒模型 页面上显示的每个元素(包括内联元素)都可以看作一个盒子,即盒模型( box model ).请看Chrome DevTools 里的截图: 可以显而易见的看出盒模型由 4 部分组成.从内到外分 ...

  10. wslgit

    @echo off setlocal enabledelayedexpansion set command=%* If %PROCESSOR_ARCHITECTURE% == x86 ( echo | ...