题目

https://leetcode.com/problems/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 longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: 3

Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: 5

思路

可以用BFS/DFS

BFS核心模板

Queue q=new LinkedList

q.add(初始点)

while(!q.isempty){

   q=q.size();

  for(int i=0;i<q;i++){

    cur=q.remove() 提出当前元素

    广度优先遍历,不断加进新元素(q不为空时一直在while,为空时证明已经遍历完成)

    q.add(初始点附近符合要求的节点)

  }

  这里写遍历过程中想记录更改什么

}

代码

BFS

    public int maxDepth(Node root) {
//bfs
if (root==null)
return 0;
int depth=0; Queue<Node> q=new LinkedList<>();
q.offer(root);//加入初始点 while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
Node curNode=q.remove();//提取出当前节点
for(Node child:curNode.children){
q.offer(child);//新加入满足条件的点
}
}
depth++;
}
return depth;
}

DFS

    int max_depth=0;
public int maxDepth(Node root) {
dfs(root,1);
return max_depth;
} public void dfs(Node node,int curDepth){
if (node==null)
return; max_depth=Math.max(max_depth,curDepth); for (Node child:node.children){
dfs(child,curDepth+1);//每次depth+1
} }

[Leetcode 559]N叉树的最大深度Maximum Depth of N-ary Tree DFS/BFS模板的更多相关文章

  1. Java实现 LeetCode 559 N叉树的最大深度(遍历树,其实和便利二叉树一样,代码简短(●ˇ∀ˇ●))

    559. N叉树的最大深度 给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不 ...

  2. Leetcode 559. N叉树的最大深度

    题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...

  3. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  4. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  5. LeetCode:N叉树的最大深度【559】

    LeetCode:N叉树的最大深度[559] 题目描述 给定一个N叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度, ...

  6. Leetcode:559. N叉树的最大深度

    Leetcode:559. N叉树的最大深度 Leetcode:559. N叉树的最大深度 Talk is cheap . Show me the code . /* // Definition fo ...

  7. [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  8. [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  10. 559. N叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000. 树的节点 ...

随机推荐

  1. js过滤掉指定html标签

    替换标签 var str = "<p><span style='color:#ccc;'>这是测试标签</span><span>这是测试htm ...

  2. CentOS 6.7 hadoop free版本Spark 1.6安装与使用

    最近的工作主要围绕文本分类,当前的解决方案是用R语言清洗数据,用tm包生成bag of words,用libsvm与liblinear训练模型.这个方案可以hold住6/70万的训练集: LIBLIN ...

  3. centos 更新git

    yum remove git rpm -ivh http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1. ...

  4. GSON 特殊类型支持序列化和反序列化,如LocalDateTime

    GSON 特殊类型支持序列化和反序列化,如LocalDateTime DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern ...

  5. ABAP 删除内表重复数据

    SORT <内表> BY <字段> [ascending/descending]. DELETE ADJACENT DUPLICATES FROM <内表> COM ...

  6. (转).Net Web开发技术栈

    作者:小曾出处:http://www.cnblogs.com/1996V/p/7700087.html 有很多朋友有的因为兴趣,有的因为生计而走向了.Net中,有很多朋友想学,但是又不知道怎么学,学什 ...

  7. 解决idea不能自动下载maven配置文件pom.xml下的jar包依赖的问题

    表现:无法下载pom配置文件中的依赖包,或只能下载少数包,各项配置都正确的情况 理由未知: 百度了很长一段时间,网上给出比较精准的解决之一是 setting>>maven>>去 ...

  8. Oracle 临时表空间暴满的原因与解决方法

    Oracle临时表空间主要用来做查询和存放一些缓冲区数据.临时表空间消耗的主要原因是需要对查询的中间结果进行排序. 重启数据库可以释放临时表空间,如果不能重启实例,而一直保持问题sql语句的执行,te ...

  9. 语言-页面-模板-thymeleaf

    一.语法 二.使用 Thymeleaf入门到吃灰 - 鞋破露脚尖儿 - 博客园 (cnblogs.com)

  10. Mac 下的虚拟机Parallels_Desktop_15

    Mac 下的虚拟机Parallels_Desktop_15 1,取得 Mac Parallels_Desktop_15.dmg 后挂载,密码:7410   2,点关闭!关闭!关闭!,千万不要点&quo ...