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. python之循环(增删)内使用list.remove()

    dat=['] for item in dat: ': dat.remove(item) print(dat) #按要求是把'0'都删掉的,输出结果是['1', '2', '3', '0'] ?? 首 ...

  2. java基础(四):谈谈java中的IO流

    1.字节流 1.1.字节输出流output 1.1.1.数据写入文件中 通过api查找output.找到很多,其中java.io.OutputStream,OutputStream: 输出字节流的超类 ...

  3. arcgis api 3.x for js 入门开发系列十六迁徙流动图

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  4. jqery autocomplete 动态传递参数的问题

    今天弄一个autocomplete 向后后台动态传递参数的问题 老的写法: params: { "saleid": $("#divSalesman input[field ...

  5. ClickOnce一项Winform部署

    先建一个Winform 控制台程序 建好后从工具箱里拖出来个 文本框 在属性中把TEXT改了 鼠标放到项目上点击右键——>属性 如下图所示,有两个发布位置. 发布位置可以选择本地文件夹,也可以选 ...

  6. c/c++ 模板 类型推断

    模板类型的推断 下面的函数f是个模板函数,typename T.下表是,根据调用测的实参,推断出来的T的类型. 请注意下表的红字部分, f(T&& t)看起来是右值引用,但其实它会根据 ...

  7. Linux(Manjaro) -Docker 安装及基本配置

    Linux(Manjaro) -Docker 安装及基本配置 基本安装 # Pacman 安装 Docker sudo pacman -S docker # 启动docker服务 sudo syste ...

  8. ubuntu 安装 google Gtest [转]有效性待验证

    最近在做一些东西,用过gtest,废话少说,现讲其再ubuntu上安装的 方法贴出来,以供朋友们参考: 安装gtest分三步: 1.安装源代码 在ubuntu的桌面上,右键选择打开终端,在终端中输入如 ...

  9. [LeetCode] 23. 合并K个排序链表

    题目链接: https://leetcode-cn.com/problems/merge-k-sorted-lists/ 题目描述: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂 ...

  10. 逆向学习-Upack的PE文见头分析

    重叠文件头 MZ文件头与PE文件头重叠. offest 0 e_magic:magic number = 4D5A('MZ') offest 3C  e_lfanew:File address of ...