题目要求

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.

题目分析及思路

题目给出一个N叉树,要求得到它的最大深度。该最大深度为根结点到最远叶结点的结点数。可以使用递归,遍历孩子结点。

python代码​

"""

# Definition for a Node.

class Node:

def __init__(self, val, children):

self.val = val

self.children = children

"""

class Solution:

def maxDepth(self, root):

"""

:type root: Node

:rtype: int

"""

if not root:

return 0

elif not root.children:

return 1

else:

c = []

for child in root.children:

c.append(self.maxDepth(child))

c.sort()

return 1 + c[-1]

LeetCode 559 Maximum Depth of N-ary Tree 解题报告的更多相关文章

  1. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. (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 ...

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

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

  6. [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 longe ...

  7. [leetcode] 559. Maximum Depth of N-ary Tree (easy)

    原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...

  8. 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)

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

  9. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

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

随机推荐

  1. Linux 下mysql的定时备份

    在实际项目中,数据库是要经常备份的,就是为了防止突发情况,前段时间,我的数据库就遭遇了入侵要支付B特比的,结果数据全没了,哎,还好当时只是个测试库,不过有了这次危机,也就开始意识到了这个问题了. 先写 ...

  2. [译]Godot 引擎 GDNative 架构初探

    GDNative的架构从最早叫"DLScript"的时候到目前为止已经发生了很大的变化.随着Godot 3.0版本接近最终发布以及API越来越稳定,是时候对GDNative目前的形 ...

  3. 新的时代:今日三款新IM正式宣战微信!

    今天(2019年1月5日)是社交圈的大日子,在今天上午将有三款不同的社交软件进行发布会,王欣.张一鸣.罗永浩旗下公司三款社交产品于今日同日发布. 新的时代,共同挑战微信 2019年1月15日,张一鸣的 ...

  4. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  5. easyui 表格底部加合计

    function setTotal() { var userid = $("#hd_buildUser").val(); $("#totalSum").data ...

  6. [Converge] Gradient Descent - Several solvers

    solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’ Algorithm to use in the op ...

  7. python 字符串编码解码和格式化问题

    转自:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868191962 ...

  8. linux log

    adb shell logcat GOODIX:v *:s cat /proc/kmsg | grep "<<" ./cbootimg.sh adb shell get ...

  9. js替换元素与设置时间间隔

    var lastReportTime = 0; //设置时间间隔 window.onload = function(){ setInterval(handleRefresh, 3000); } fun ...

  10. Git版本控制工具的简单使用

    1.下载gitd客户端,注册github账号. 2.本地生成公钥和私钥,并将公钥粘贴到github上,测试连接. 3.先pull,从远程服务器中下载项目文件,然后再pushi,提交至服务器. 4. g ...