leetcode刷题-559. Maximum Depth of N-ary Tree
题目: 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的更多相关文章
- [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 ...
- [刷题] 104 Maximum Depth of Binary Tree
要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...
- C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- 559. Maximum Depth of N-ary Tree - LeetCode
Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
随机推荐
- linux 基础10-磁盘配额管理
1. 基本概念 1.1 概念: 在linux系统中,由于是多人多任务的使用环境,所以会有多人共同使用一个硬盘空间的情况,如果其中少数几个人大量使用了硬盘空间的话,势必会压缩其他使用者的使用空间,因此管 ...
- 深度学习—从LeNet到DenseNet
CNN从90年代的LeNet开始,21世纪初沉寂了10年,直到12年AlexNet开始又再焕发第二春,从ZF Net到VGG,GoogLeNet再到ResNet和最近的DenseNet,网络越来越深, ...
- How to resolve emulator cannot be launched issue by command line
Issue: Resolution: 1. Open the system variables, find the Path and edit it, add below item : C:\User ...
- python_面向对象——对象之间的关联关系
1.将类中的对象关联起来(简单的方法) class Person: def __init__(self,name,age,sex): self.name = name self.age = age s ...
- hbase实践之数据读取详解
hbase基本存储组织结构与数据读取组织结构对比 Segment是Hbase2.0的概念,MemStore由一个可写的Segment,以及一个或多个不可写的Segments构成.故hbase 1.*版 ...
- 对Ajax的一些理解
前言 在学习js的过程,我个人对于Ajax这项技术有着很深的印象,大概是因为它在我们网页开发过程中常见的交互里所发挥的作用太过于关键了吧,所以我想在这里好好地谈一些自己对它的理解. 概念理解 Ajax ...
- stm32中阻塞模式和非阻塞模式 in blocking mode 与 in non-blocking mode区别
阻塞模式和非阻塞模式...... 我的理解是:阻塞模式就像是一个延时函数,当这个函数没处理完那么,所有的按照流程需要执行的代码都不会被执行,要等到这个延时完成,类似 平时看书上写的LED灯闪烁,用的d ...
- nodejs,express链式反应
链式反应--next() const myexpress = require('express'); const bodyparser = require('body-parser'); var se ...
- Boostnote跨平台 Markdown 编辑器
Boostnote 0.11.6 发布了,Boostnote 是一个 Markdown 编辑器,可用于 Mac.Windows.Linux.Android 和 iOS 平台. 新版特性 Dev: 更新 ...
- git commit 详解
git commit 命令用来将本地暂存的修改提交到版本库. git commit -m '提交信息'. 我们用的比较多的应该就是-m 参数.添加提交信息. 经常手抖的同学可能会知道,git comm ...