题目

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:

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

Example 2:

  1. 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]
  2. 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

  1. public int maxDepth(Node root) {
  2. //bfs
  3. if (root==null)
  4. return 0;
  5. int depth=0;
  6.  
  7. Queue<Node> q=new LinkedList<>();
  8. q.offer(root);//加入初始点
  9.  
  10. while(!q.isEmpty()){
  11. int size=q.size();
  12. for(int i=0;i<size;i++){
  13. Node curNode=q.remove();//提取出当前节点
  14. for(Node child:curNode.children){
  15. q.offer(child);//新加入满足条件的点
  16. }
  17. }
  18. depth++;
  19. }
  20. return depth;
  21. }

DFS

  1. int max_depth=0;
  2. public int maxDepth(Node root) {
  3. dfs(root,1);
  4. return max_depth;
  5. }
  6.  
  7. public void dfs(Node node,int curDepth){
  8. if (node==null)
  9. return;
  10.  
  11. max_depth=Math.max(max_depth,curDepth);
  12.  
  13. for (Node child:node.children){
  14. dfs(child,curDepth+1);//每次depth+1
  15. }
  16.  
  17. }

[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. 扩linux 主分区 xfs 格式的

    在centos8 上 扩主分区 # 扩系统盘,第一个分区 growpart /dev/vda 1 #后面加挂接点 一般是 / xfs_growfs  / # 查看 df -h

  2. vue高级进阶( 三 ) 组件高级用法及最佳实践

      vue高级进阶( 三 ) 组件高级用法及最佳实践 世界上有太多孤独的人害怕先踏出第一步. ---绿皮书 书接上回,上篇介绍了vue组件通信比较有代表性的几种方法,本篇主要讲述一下组件的高级用法和最 ...

  3. Uri转绝对路径工具类

    /** * 反射从 provider uri中获取 文件绝对路径 * @param context * @param uri * @return */ private static String ge ...

  4. ssh 登陆 Host key verification failed.

    报错 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION ...

  5. 2023年3月1日其它etf大涨,医疗未涨,3月2日会补涨,涨0.5%卖出。

  6. import cv2时出现ImportError: DLL load fail:找不到指定模块

  7. disp

    str = sprintf( 'Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse,bestc,bestg); disp(s ...

  8. 区分CommonJs/ES6 Module/AMD/CMD

    模块加载方式 CommonJs ES6 Module AMD CMD UMD Commonjs和ES6 Module的区别 总结 1.CommonJS CommonJS 是一个项目,其目标是为 Jav ...

  9. vue axios传值到后端报错问题

    原因:axios使用post方法传输数据给后端默认为json格式,会导致跨域问题 原理:qs是查询字符串解析和将对象序列化的库 qs.stringify()   将对象序列化成url的形式,以& ...

  10. 安卓开发学习10-1:数据存储:Shared Preferences存储

    解析 什么是Shared Perferences 应用场景 配置信息 主题信息 游戏的积分信息等 存储路径 在本应用中的data-data-应用包-自定义名称xml文件下保存写入的数据信息 使用 获取 ...