[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 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. 
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的更多相关文章
- [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 ...
 - [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. ...
 - leetcode刷题-559. Maximum Depth of N-ary Tree
		
题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...
 - [LeetCode&Python] Problem 53. Maximum Subarray
		
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
 - [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 ...
 - [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 ...
 - 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 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 ...
 
随机推荐
- 已使用 163 邮箱测试通过,且支持 SSL 连接。  发送邮件
			
示例:Jack 发送一封邮件给 Rose. public class SendMail { public static void main(String[] args) { b ...
 - 《剑指offer》第二十一题(调整数组顺序使奇数位于偶数前面)
			
// 面试题21:调整数组顺序使奇数位于偶数前面 // 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有 // 奇数位于数组的前半部分,所有偶数位于数组的后半部分. #inclu ...
 - socket+django
			
1.socket 网络上任意两个程序之间要进行通信,需要依靠socket(端口).socket封装了TCP/IP协议,让网络通信基于TCP/IP协议的形式实现. socket可以翻译为插座,那么一个服 ...
 - 源代码方式调试Mycat
			
如果是第一次刚接触MyCat建议下载源码在本地通过eclipse等工具进行配置和运行,便于深入了解和调试程序运行逻辑. 1)源代码方式调试与配置 由于MyCat源代码目前主要托管在github上,大家 ...
 - Maven部署web应用到远程服务器
			
Maven部署web应用到远程服务器 找到了一个很详细的地址:http://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tom ...
 - Java 的对象和类
			
Java 是一种面向对象的语言.作为一个面向的语言,Java 具有面向对象的特性,Java 能够支持下面的一些基本概念 − 多态(Polymorphism) 继承(Inheritance) 封装(En ...
 - H5表单基础知识(二)
			
表单新增属性 <!--<input type="text" class="name" />--> <!-- placeholder ...
 - PHP header函数设置http报文头示例详解
			
//定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...
 - splunk LB和scale(根本在于分布式扩展index,search)
			
Forwarder deployment topologies You can deploy forwarders in a wide variety of scenarios. This topic ...
 - vue-cli的安装及使用
			
一. node 和npm 1.在安装vue-cli前,要确认自己的电脑是否安装了node和npm 2.查询版本如下(vue脚手架支持node@4.xx以上) node -v 查询node版 ...