[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 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:
- The depth of the tree is at most
1000. - 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的更多相关文章
- (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 ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode] 559. Maximum Depth of N-ary Tree (easy)
原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...
- 559. Maximum Depth of N-ary Tree - LeetCode
Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...
- 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 ...
- [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 ...
随机推荐
- ros查看摄像头是否打开正常
使用rqt_image_view命令,查看摄像头是否正常输出图像
- Win10正式版U盘安装教程
1.首先我们需要登陆“微软中国下载中心”,从中下载一款名为“MediaCreationTool”的工具,利用该工具可以制作Win10安装U盘.直接通过以下地址快速进入“Windows下载中心”,根据自 ...
- MySQL5.6的4个自带库详解
MySQL5.6的4个自带库详解 1.information_schema详细介绍: information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式.什么是元数据呢?元数 ...
- [No000011C]使人醒悟的生活中的定律
1. 墨菲定律 我们在事前应该是尽可能想得周到.全面一些,如果真的发生不幸或者损失,就笑着应对吧,关键在于总结所犯的错误,而不是企图掩盖它. 2. 羊群效应 对他人的信息不可全信也不可 ...
- tensorflow的ckpt文件总结
1.TensorFlow的模型文件 --checkpoint_dir | |--checkpoint | |--MyModel.meta | |--MyModel.data-00000-of-0000 ...
- idea 乱码问题
1. db browser查询结果为乱码: 找到idea的安装目录 如C:\..\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\ ...
- CSS:元素类型
元素分类 在CSS中,html中的标签元素大体分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 块状元素 <div>.<p>.<h1>...& ...
- Angular4 Ng 模块
原文地址 http://www.jianshu.com/p/317f0e4b7284 根模块 (root module) 每个应用都至少有一个根模块用来引导并运行应用.根模块通常命名为 AppModu ...
- 创建本地SVN版本库以及将SVN导入GIT
创建本地SVN 通常SVN作为一种服务,是在服务器上架设,供用户通过网络访问使用.但如果只是自己日常使用,完全可以架设在本机上,不需要启动后台程序,通过文件的方式访问即可. 建立本地SVN非常简单,一 ...
- 游戏引擎架构 && windows 核心编程
欲想正人,必先正己. 静坐当思己过,闲谈莫论人非. 垂直同步的作用: 为避免画面撕裂,许多渲染引擎会在交换缓冲区之前,等待显示器的垂直区间消隐,即电子枪重归屏幕上角的时间. 高分辨率计时器的时间漂移 ...