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下zookeeper集群搭建
Linux下zookeeper集群搭建 部署前准备 下载zookeeper的安装包 http://zookeeper.apache.org/releases.html 我下载的版本是zookeeper ...
- Mybatis在xml文件中处理大于、小于、不等于号的方法
在mapper.xml使用大于.小于等符号会和xml语法冲突,解决冲突有两种方式. 方法一: 使用转义字符: 字符名称 字符符号 转义字符 大于号 > > 小于号 < < 与 ...
- Python计算AUC
AUC(Area under curve)是机器学习常用的二分类评测手段,直接含义是ROC曲线下的面积.另一种解释是:随机抽出一对样本(一个正样本,一个负样本),然后用训练得到的分类器来对这两个样本进 ...
- #Python绘制 文本进度条,带刷新、时间暂缓的
#Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx ...
- 00_UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: # <class 'django.contrib.auth.models.Group'> QuerySet.
访问groups时,后端报警告 UnorderedObjectListWarning: Pagination may yield inconsistent results with an unorde ...
- 多线程爬取猫眼电影TOP100并保存到mongo数据库中
import requests import re import json from requests.exceptions import RequestException from multipro ...
- 分布式锁的三种实现方式 数据库、redis、zookeeper
版权声明: https://blog.csdn.net/wuzhiwei549/article/details/80692278 一.为什么要使用分布式锁 我们在开发应用的时候,如果需要对某一个共享变 ...
- 应用webservice实现公网天气查询
1. wsdl网址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 2. URL:http://www.webxml.com.cn/zh ...
- Codevs 1038 一元三次方程求解 NOIP 2001(导数 牛顿迭代)
1038 一元三次方程求解 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 有形如:ax3+b ...
- GAN生成式对抗网络(二)——tensorflow代码示例
代码实现 当初学习时,主要学习的这个博客 https://xyang35.github.io/2017/08/22/GAN-1/ ,写的挺好的. 本文目的,用GAN实现最简单的例子,帮助认识GAN算法 ...