第一次写出了具有迭代和递归的函数,还是有点收获的,虽然题目比较简答

当要对某些对象重复使用时,考虑循环,也就是迭代

当函数可以简化一个重复的操作时,考虑递归,而且就当下一次使用这和函数的结果已经有啦,就不会考虑的太复杂

自己写的答案:

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root:
return 0
if not root.children:
return 1
sub_max=[]
for child in root.children:
sub_max.append(self.maxDepth(child)+1)
return max(sub_max)

反思学习:

1.对list不知道是None还是[ ]的时候,使用 if not list

2.list 添加元素使用append

3.list取最值max

优秀答案:

class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root:
return 0
if not root.children:
return 1
depth = 1 + max(self.maxDepth(child) for child in root.children)
return depth

没有使用list,很简洁

"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root: return 0
depth = 0
que = collections.deque()
que.append(root)
while que:
size = len(que)
for i in range(size):
node = que.popleft()
for child in node.children:
que.append(child)
depth += 1
return depth

使用了队列,层序遍历记录层数

LeetCode559. Maximum Depth of N-ary Tree的更多相关文章

  1. Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 说明: 树的深度不会超过 1000. 树的节点总不会超过 5000. class Solution { ...

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  3. LeetCode 104. Maximum Depth of Binary Tree

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

  4. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  5. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  6. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  7. Leetcode Maximum Depth of Binary Tree

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

  8. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  9. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

随机推荐

  1. linux基础学习之软件安装以及常用命令(三)

    添加和查看用户: [root@localhost ~]# useradd anderson [root@localhost ~]# cat /etc/passwd 显示如下: [root@localh ...

  2. CF 827E Rusty String FFT

    传送门 如果没有碍事的?的话,判定字符串的循环节直接用KMP的失配数组就可以搞定.现在有了碍事的?,我们就需要考虑更通用的算法. 考虑KMP失配数组判定字符串循环节的本质,发现判定\(k\)是否为字符 ...

  3. css-使不同大小的图片在固定大小的容器中居中

    HTML示例如下: <ul> <li class="imgbox"><img src="img1.jpg"></li& ...

  4. HTML利用posotion属性定位 小技巧

    1.居中效果 父级DIV (index-top )属性设置为 text-align:center; 子级DIV( tabIndex-main)属性设置为 margin:0 auto;   2.左右对齐 ...

  5. 64位版本的Windows不兼容,masm无法运行解决方法

    问题: 在Window64位运行不了的masm 解决方法: 1.下载DosBox0.74(当前最新): 2.安装后运行,运行后出现控制台: 3.在DosBox的控制台下运行 Mount x: x:/m ...

  6. 或许你并不需要jQuery

    此文为翻译文章,原文链接:you might not need jquery jQuery 和它的相关插件都是很强大的,使用它们让我们的应用开发变得简单.如果你正在开发另一个库,请花点时间思考以下,你 ...

  7. CSS 0.5px 细线边框的原理和实现方式

    细线边框的具体实现方法有:伪元素缩放或渐变,box-shadow模拟,svg画线,border-image裁剪等.要实现小于1px的线条,有个先决条件:屏幕的分辨率要足够高,设备像素比要大于1,即cs ...

  8. Java XML SAX 解析注意

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4002738.html 什么时候可以把解析值赋给对象 一般从网 ...

  9. sql 字符串函数、数学函数

    -- 字符函数:-- 查询结果姓名小写 select lower(ename), sal, job from emp;-- 查询结果姓名大写 select upper(ename), sal, job ...

  10. mybatis resultMap 映射配置

    现有数据库表: CREATE TABLE `dept_p` ( `DEPT_ID` ) NOT NULL, `DEPT_NAME` ) DEFAULT NULL, `PARENT_ID` ) DEFA ...