(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 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.
--------------------------------------------------------------------------------------------------------------------------------------
这个和求二叉树的最大深度类似,只要掌握了二叉树的最大深度的解法以及理解了N叉树的原理,解决这个题就会很简单了。
递归/DFS:
C++代码:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if(!root) return ;
int maxSum = ; //这个是必须的,不能写错。因为根节点的深度为1。
for(Node *cur:root->children){
maxSum = max(maxSum, + maxDepth(cur));
}
return maxSum;
}
};
BFS(迭代):
C++代码:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if(!root) return ;
queue<Node*> q;
q.push(root);
int sum = ;
while(!q.empty()){
sum++;
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
for(Node *cur : t->children){
q.push(cur);
}
}
}
return sum;
}
};
(N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode] 559. Maximum Depth of N-ary Tree (easy)
原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...
- 559. Maximum Depth of N-ary Tree - LeetCode
Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...
- (二叉树 BFS DFS) leetcode 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】559. Maximum Depth of N-ary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 longe ...
随机推荐
- 服务器端Session和客户端Session
客户端Session和服务器端Session 当用户首次与web服务器建立连接的时候,服务器会给用户分发一个SessionID作为标识.SessionID是一个由24个字符组成的随机字符串.用户每次提 ...
- NET4.6下的UTC时间转换
int UTCSecond = (int)((DateTimeOffset)DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local)).ToUnix ...
- ArcPy 创建图层空间索引
使用Python脚本进行图层的空间索引的创建. 附上Python代码: # -*- coding: utf-8 -*- # nightroad import sys import arcpy relo ...
- python内存回收的问题
python实际上,对于占用很大内存的对象,并不会马上释放. 举例,a=range(10000*10000),会发现内存飙升一个多G,del a 或者a=[]都不能将内存降下来.. del 可以删除多 ...
- 【内容】MVP 三剑客活动
最近微软搞了一个活动,叫做三剑客,主旨就是“Cloud+AI本地化社区活动,为微软产品本地化做出自己的贡献”,虽然已是rMVP,但也同样收到的社区经理的来信,本人也报名参加了这个活动,同时给了我三个小 ...
- JS 转换HTML转义符
JS转换HTML转义符 //去掉html标签 1 2 3 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?& ...
- 11 Django RESTful framework 实现缓存
01-安装 pip install drf-extensions 02-导入 from rest_framework_extensions.cache.mixins import CacheRespo ...
- scala的多种集合的使用(3)之遍历集合的方法
遍历集合的方法 1.用foreach循环遍历一个集合 foreach接收一个函数作为参数.定义的函数应该接收一个元素作为输入参数,然后不要返回任何的东西.输入的参数的类型应该匹配集合中的类型.随着fo ...
- 菜鸟学IT之python词云初体验
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2822 1. 下载一长篇中文小说. 2. 从文件读取待分析文本. txt = ...
- Windows Server 2008 R2提示api-ms-win-crt-runtime-l1-1-0.dll 丢失解决方法
在一台服务器的全新windows Server 2008 R2 服务器上配置php运行环境, 在启动Apache之后,显示下图错误: 解决方法: 1.安装VC redit.exe程序解决 是VC的一个 ...