题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/

n-ary-tree的数据结果表示

// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};

  DFS算法:迭代

int maxDepth(Node* root) {
if (root == NULL) return ;
int max = , n = root->children.size();
for (int i = ; i<n; i++)
{
int temp=maxDepth(root->children[i]);
if (temp>max)
max = temp;
//max=std::max(max,maxDepth(root->children[i]));
}
return max + ;
}

BFS算法:队列

int maxDepth(Node* root) {
if (root == NULL) return ;
int res=;
queue<Node*> Q;
Q.push(root);
while(!Q.empty())
{
res++;
int n=Q.size();
for(int i=;i<n;i++)
{
Node* t=Q.front(); for(int j=;j<t->children.size();j++)
Q.push(t->children[j]);
Q.pop();
}
}
return res;
}

leetcode刷题-559. Maximum Depth of N-ary Tree的更多相关文章

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

  2. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  3. C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...

  4. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

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

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

  6. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  7. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  8. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. 09_Azkaban案例实践2_Command多job工作流flow

    1.Command类型多job工作流flow 1.创建有依赖关系的多个job描述:第一个job:foo.job # foo.job type=command command=echo foo 2.第二 ...

  2. Linux的awk 中的while do-while for循环

    linux awk的 while.do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出.break中断当前正在执行的循环并跳到循环外 ...

  3. Error creating bean with name 'objectMapperConfigurer' defined in class path resource

  4. 各位大佬Python的第一部分道基础题已经整理好了,希望大家面试的时候能用的上。

    Python的第一部分道基础题,希望大家面试的时候能用的上. 1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途 ...

  5. Hadoop的简单了解与安装

    hadoop 一, Hadoop  分布式 简介Hadoop  是分布式的系统架构,是  Apache  基金会顶级金牌项目 分布式是什么?学会用大数据的思想来看待和解决问题 思 想很重要 1-1 . ...

  6. 【H5】 经纬度位置获取navigator.geolocation.getCurrentPosition

    navigator.geolocation.getCurrentPosition(function(){})经度 : coords.longitude 纬度 : coords.latitude 准确度 ...

  7. httprunnermanager环境搭建 -----转

    转发自  https://www.cnblogs.com/tiechui2015/p/10017801.html  感谢大神 1,开源代码下载 这里的git下载地址是:https://github.c ...

  8. 甘特图控件如何自定义绘图?DevExpress Winforms帮你忙

    DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...

  9. combogrid下拉方法封装

    //html <input id="addxxxxx" name="xxxxxx" style="width:380px;height:36px ...

  10. 关于EMF中从schema到ecore转变中的默认处理问题

    以前的工作,建模基本都是通过ecore tool直接画ecore的模型图来完成,最近要从schema创建ecore文件,本来以为是非常简单的一件事情,使用向导创建genmodel,然后从xsd文件导入 ...