Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
说明:
- 树的深度不会超过 1000。
- 树的节点总不会超过 5000。
class Solution {
public:
int maxDepth(Node* root) {
if(root == NULL)
return 0;
int cnt = 0;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
int s = q.size();
cnt++;
while(s--)
{
Node* node = q.front();
q.pop();
for(int i = 0; i < node ->children.size(); i++)
{
if(node ->children[i] != NULL)
{
q.push(node ->children[i]);
}
}
}
}
return cnt;
}
};
Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度的更多相关文章
- [LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- 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 ...
- 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 ...
- LeetCode559. Maximum Depth of N-ary Tree
第一次写出了具有迭代和递归的函数,还是有点收获的,虽然题目比较简答 当要对某些对象重复使用时,考虑循环,也就是迭代 当函数可以简化一个重复的操作时,考虑递归,而且就当下一次使用这和函数的结果已经有啦, ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- (N叉树 DFS 递归 BFS) 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 ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
- C#LeetCode刷题之#559-N叉树的最大深度(Maximum Depth of N-ary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4088 访问. 给定一个 N 叉树,找到其最大深度. 最大深度是指 ...
- C#LeetCode刷题之#104-二叉树的最大深度(Maximum Depth of Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4072 访问. 给定一个二叉树,找出其最大深度. 二叉树的深度为根 ...
随机推荐
- Java内存问题:java.lang.OutOfMemoryError: PermGen space
代码运行环境: jdk1.7.0_25 apache-tomcat-8.0.30 详细报错日志: org.springframework.web.util.NestedServletException ...
- 怎么取消主键自增长的问题(SQL Server)
以前经常会碰到这样的问题,当我们删除了一条自增长列为1的记录以后,再次插入的记录自增长列是2了.我们想在插入一条自增长列为1的记录是做不到的.我们可以通过设置SET IDENTITY_INSERT & ...
- 嘴巴题9 Codeforces 453A. Little Pony and Expected Maximum
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- 从虚拟机视角谈 Java 应用性能优化
从虚拟机视角谈 Java 应用性能优化 周 祥, 软件工程师, IBM 简介:Java 的普及和广泛应用,以及其基于虚拟机运行的机制,使得性能问题越来越重要.本文从 Java 虚拟机的角度,特别是垃圾 ...
- 阿里云OSS简单上传本地文件
上传本地文件 # -*- coding: utf-8 -*- import oss2 # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM账号进行API访问 ...
- Python3基础笔记_迭代器
# Python3 迭代器与生成器 import sys ''' 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开 ...
- 【玲珑杯 round#18 A】计算几何你瞎暴力
[Link]:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 [Description] [Solution] 因为每个点的(xi ...
- 2-sat——暴力染色输出方案hdu1814
因为要求输出字典序最小的解,所以用暴力染色 具体有点像二分图染色 遍历0-2*n-1个点,尝试将每个点染成1,该点所能到达的所有点都要染成1 如果不行,则把上该点的影响消除,再把对立点染成1,如果还不 ...
- select 语句中 if 的用法
IF( expr1 , expr2 , expr3 ) expr1 的值为 TRUE,则返回值为 expr2 expr1 的值为FALSE,则返回值为 expr3 例: ,); ,); ", ...
- Joomla - T3模板(非常好用的4屏响应式模板)
一.下载 T3 模板 下载地址(需要注册登录才能下载):https://www.joomlart.com/member/downloads/joomlart/t3-framework/t3-blank ...