[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 ...
随机推荐
- perl 读取一个文件 替换文件的关键词 把数据替换到新的文件
replace # replace #!/usr/bin/perl my @data = (); my ($fname ,$rp, $nfname)= @ARGV; my ($o, $n) = spl ...
- js将图片转换为base64
直接上代码: var img = "imgurl";//imgurl 就是你的图片路径 function getBase64Image(img) { var canvas = do ...
- thinkphp中setInc、setDec方法
可用于统计字段(通常是数字类型的字段)的更新,例如积分,等级,登陆次数等 必须配合连贯操作where一起使用 score 是数据库指定的某个字段 $User = M("User" ...
- PAT甲级1131 Subway Map【dfs】【输出方案】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...
- css学习_css三大特性
css三大特性 1.层叠性(就近原则) 2.继承性(和文字有关的会继承) 3.优先级 (权重问题) 权重:0,0,0,0 0001 ---标签选择器(注意:即使有20个标签选择器也不会比一个伪类选 ...
- python selenium配置
写该博客时环境 mac 10.14.1 (18B75) python 3.7 pip (不用这个就是了,用pip3) $ pip --version pip 10.0.1 from /Users/wj ...
- Hot Plug Detection, DDC, and EDID
Hot Plug Detection, DDC, and EDID DataPro Tech Info > Hot Plug Detection, DDC, and EDID Hot Plugg ...
- ORACLE DIRECTORY目录管理步骤
ORACLE DIRECTORY目录管理步骤 ORACLE的 DIRECTORY在数据库中是个目录的路径,需要在操作系统中有相应的目录与之对应:ORACLE目录的作用就是让ORACLE数据库和操作系统 ...
- MUI框架a链接href跳转失效解决方法,解决MUI页面不会滚动的方法
//解决 所有a标签 导航不能跳转页面 mui('body').on('tap','a',function(){document.location.href=this.href;}); //解决MUI ...
- linux crypt()函数使用总结
原型: char *crypt(const char *key, const char *salt); 标准说明: crypt()算法会接受一个最长可达8字符的密钥(即key),并施以数据加密算法(D ...