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-两个排序数组 ...
随机推荐
- C 格式化的输入输出(printf scanf)
- 左对齐 (默认右对齐) printf("%-9d\n",123); 123 printf("%9d\n",123); 123 printf ...
- 文件格式 rdp
auto connect:i:1full address:s:公网访问IP地址username:s:Administrator
- PAT Basic 1074 宇宙无敌加法器 (20 分)
地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星人都必须熟记各位数字的进制 ...
- 【jmeter】无GUI界面,命令行运行测试脚本
一.应用场景 1.无需交互界面或受环境限制(linux text model) 2.远程或分布式执行 3.持续集成,通过shell脚本或批处理命令均可执行,生成的测试结果可被报表生成模块直接使用,便于 ...
- Selenium(三)webdriver的API与定位元素
在学习定位元素之前,应该要学会: 1.打开浏览器 2.打开网页 3.定位元素及操作 ①定位元素 可只此输入框的id是kw,name是wd,class是s_ipt ②在python编辑器中找到该元素 通 ...
- mysql查询表中最后一条记录
查询全部的记录: select * from test_limit ; 查第一条记录: select * from test_limit limit 1; ...
- python_tkinter弹出对话框1
tkinter提供了三个模块,可以创建弹出对话窗口:(使用必须单独导入模块) 1.messagebox 消息对话框 示例:askokcancel import tkinter # 导入消息对话框子模块 ...
- SVN(subversion )服务端和客户端的下载安装使用
SVN(subversion)官方网站: http://subversion.apache.org/packages.html 一.服务端下载 1.百度搜关键字:subversion 进入subve ...
- python的变量命名规范
一.python变量名命名规则: 1.变量名通常由字母,数字,下划线组成; 2.数字不能作为变量名开头; 3.不能以python中的关键字命名; 4.变量名要有意义; 5.不要用汉字和拼音去命名; 6 ...
- selenium报错以及各解决方法
1.driver.findElement(By.name("wd")).sendKeys("selenium"); 报错:The method sendKeys ...