LeetCode559. Maximum Depth of N-ary Tree
第一次写出了具有迭代和递归的函数,还是有点收获的,虽然题目比较简答
当要对某些对象重复使用时,考虑循环,也就是迭代
当函数可以简化一个重复的操作时,考虑递归,而且就当下一次使用这和函数的结果已经有啦,就不会考虑的太复杂
自己写的答案:
"""
# 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的更多相关文章
- Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度
给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 说明: 树的深度不会超过 1000. 树的节点总不会超过 5000. class Solution { ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Javascript中的bind详解
前言 用过React的同学都知道,经常会使用bind来绑定this. import React, { Component } from 'react'; class TodoItem extends ...
- iphone设置fiddler代理测试
iPhone上配置fiddler为代理方法: 打开IPhone, 找到你的网络连接,打开HTTP代理,输入Fiddler所在机器的IP地址(比如:192.168.1.104) 以及Fiddler的端口 ...
- 通过代码动态创建IIS站点
对WebApi进行单元测试时,一般需要一个IIS站点,一般的做法,是通过写一个批处理的bat脚本来实现,其实通过编码,也能实现该功能. 主要有关注三点:应用程序池.Web站点.绑定(协议类型:http ...
- 聊一聊数组的map、reduce、foreach等方法
聊聊数组遍历方法 JS 数组的遍历方法有好几个: every some filter foreach map reduce 接下来我们来一个个地交流下. every() arr.every(callb ...
- Oracle EBS 验证应收是否等于加税
--To validate whether a transaction's REC is equal to its REV plus TAX or not --验证应收是否等于收入加税 SELECT ...
- a标签 按钮化使用
a标签 按钮化使用 a href="javascript:void(0);" onclick="js_method()" a href="javasc ...
- 给UIScrollView添加category实现UIScrollView的轮播效果
给UIScrollView添加category实现UIScrollView的轮播效果 大家都知道,要给category添加属性是必须通过runtime来实现的,本教程中给UIScrollView添加c ...
- C 转义字符说明
%a(%A) 浮点数.十六进制数字和p-(P-)记数法(C99)%c 字符%d 有符号十进制整数%f 浮点数(包括float和doulbe)%e(%E) 浮点数指数输出[e-(E-)记数法]%g(%G ...
- 局域网不同网段访问设置WINS域名服务系统
大背景 公司两台路由器,网段不同 路由器:192.168.0.1 路由器:192.168.1.1 路由器2需要访问路由器1的机子,初始是ping不通的. 方案 使用IP设置里WINS设置,即可轻松实现 ...
- Linux 环境部署记录(三) - Jenkins安装与配置
Jenkins安装 为了兼容生产环境的jdk1.7版本,从官网得知,Jenkins必须是1.6之前的版本,因此下载jenkins-1.596.3-1.1.noarch.rpm到本地进行安装: #移动到 ...