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.

DFS:

"""
# 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
"""
maxD=0 if root:
for c in root.children:
maxD=max(self.maxDepth(c),maxD) return maxD+1
return maxD

  

BFS:

"""
# 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
""" queue=[root]
de=0 if root:
while queue:
de+=1
for i in range(len(queue)):
node=queue.pop(0)
for c in node.children:
queue.append(c) return de

  

[LeetCode&Python] Problem 559. Maximum Depth of N-ary Tree的更多相关文章

  1. [LeetCode&Python] Problem 104. Maximum Depth of Binary Tree

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

  2. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  3. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  4. [LeetCode&Python] Problem 53. Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  5. [LeetCode&Python] Problem 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. [LeetCode&Python] Problem 700. Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  7. 559. Maximum Depth of N-ary Tree - LeetCode

    Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...

  8. 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  9. 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 ...

随机推荐

  1. Spark与Flink大数据处理引擎对比分析!

    大数据技术正飞速地发展着,催生出一代又一代快速便捷的大数据处理引擎,无论是Hadoop.Storm,还是后来的Spark.Flink.然而,毕竟没有哪一个框架可以完全支持所有的应用场景,也就说明不可能 ...

  2. 《剑指offer》第二十三题(链表中环的入口结点)

    // 面试题23:链表中环的入口结点 // 题目:一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, // 环的入口结点是结点3. #include <iostream> ...

  3. 在ANE插件中启动AIR开发的Android应用

    在Android原生应用开发中,启动一个应用非常容易: Intent __intent = new Intent(this, YourAppActivity.class); startActivity ...

  4. Spring Boot入门——文件上传与下载

    1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  5. ngIf 和 template的结合使用

    前提: 当遇到 一个种情况,一个元素中既可以显示 一个字符串变量,也可以显示一个模板 实现: // html <span class="ant-alert-message" ...

  6. vuejs2点滴

    在Vue定义一个不被添加getter setter 的属性: https://github.com/vuejs/vue/issues/1988 博客: 0.vux的x-input源码分析. http: ...

  7. php 邮件发送利器 PHPMailer

    php 自带的邮件发送函数已经弱到不能用了. PHPMailer非常的强大. 绝对是php里必须使用的程序. 下载地址: https://github.com/Synchro/PHPMailer 只要 ...

  8. Rspec: everyday-rspec实操: 第8章DRY. (6个方法,其中3个方法好上手)

    Don't Repeat Yourself. • 把操作步骤提取到辅助模块中;✅ • 通过let复用测试中的实例变量;✅ • 把通用的设置移到共享的情景中;⚠️(不喜欢) • 在RSpec和rspec ...

  9. codeforces 700a//As Fast As Possible// Codeforces Round #364(Div. 1)

    题意:n个人要运动ll长,有个bus带其中几个人,问最短时间 最后所有人在同一时间到终点是用时最少的.由于搭bus相当于加速,每个人的加速时间应该一样.先计算bus走过的路程route.看第一个人被搭 ...

  10. php--------http 状态代码及其原因

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...