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.

--------------------------------------------------------------------------------------------------------------------------------------

这个和求二叉树的最大深度类似,只要掌握了二叉树的最大深度的解法以及理解了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的更多相关文章

  1. [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 ...

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

  3. 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(N-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 (easy)

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

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

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

  7. (二叉树 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 ...

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

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

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

随机推荐

  1. Source-Based XSS Test Cases

    Single Reflection Case 01 - Direct URL Injection (no parameter) payload: https://brutelogic.com.br/x ...

  2. HTML页面转换为Sharepoint母版页(实战)

    分享人:广州华软 极简 一. 前言 SharePoint有母版页.布局页.母版页存放着如头部(顶部菜单.导航),底部等比较通用部分,通常网站只需一套即可:而布局页,则存放着主要内容部分,根据页面需要, ...

  3. 从0到1搭建AI中台

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | 宜信技术学院 作者 | 井玉欣 导读:随着“数据中台”的提出和成功实践,各企业纷纷在“大中台 ...

  4. 1.2 Cesium渲染流程

    “从前有座山,山里有座庙,庙里有个......”我们喜欢这样讲故事,有头有尾巴.Cesium实时刷新,就是说每一帧都在更新,(但这也是一般状态下,如果场景完全静悄悄也可请求渲染模式,这时就不是每一帧都 ...

  5. 关于Fragment里面嵌套fragment

    今天看到一篇好文章 https://www.2cto.com/kf/201609/545979.html 转载过来记录一下,往后需要的时候可以随时查看: 接下来进入正题: 动态fragment的使用 ...

  6. OrmLite-更符合面向对象的数据库操作方式

    1. jar包下载 下载地址:http://ormlite.com/releases/,一般用core和android包即可. 如果使用的是android studio,也可以直接通过module s ...

  7. gradle下载及配置

    windows安装 1.下载地址:http://services.gradle.org/distributions/ 2.下载**-bin.zip,解压即可 配置环境变量:gradle_home:D: ...

  8. mysql中几个日期时间类型之间的区别和使用

    MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型           含义 date                     只存 ...

  9. java.util.Arrays.useLegacyMergeSort=true 作用

    (原) 今天看了一下现场的环境,发现有个其它部门的项目用到了这样一个参数: -Djava.util.Arrays.useLegacyMergeSort=true 于是查看了一下什么作用. 在JDK1. ...

  10. linux 安装中文字体

    工具/原料   centos6.5_x64 方法/步骤    centos6.5下使用下面命令进行安装 yum install -y fontconfig mkfontscale  使用fc-list ...