题目

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.

For example, given a 3-ary tree:

We should return its max depth, which is 3.

Note:

  1. The depth of the tree is at most 1000.
  2. The total number of nodes is at most 5000.

题目地址

讲解

这道题需要对每次层的深度做个记录,我直接使用结点的val属性来记录深度。另外就是给根节点深度置为1的时候有个技巧,设置一个一次性的flag。

Java代码

/*
// Definition for a Node.
class Node {public int val;public List<Node> children;public Node() {}public Node(int _val,List<Node> _children) {val = _val;children = _children;}
};
*/
class Solution {private int result=0;private boolean flag = true;public int maxDepth(Node root) {if(root==null){return result;}if(flag){root.val=1;flag = false;}if(result<root.val){result = root.val;}for(Node node:root.children){node.val = root.val+1;maxDepth(node);}return result;}}

leetcode讲解--559. Maximum Depth of N-ary Tree相关推荐

  1. 559. Maximum Depth of N-ary Tree*

    559. Maximum Depth of N-ary Tree* https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ 题目描述 Gi ...

  2. LC 559. Maximum Depth of N-ary Tree

    559. Maximum Depth of N-ary Tree Easy 19218 1.题目描述 Given a n-ary tree, find its maximum depth. The m ...

  3. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  4. 【leetcode】104. Maximum Depth of Binary Tree

    1. 题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

  5. LeetCode 104. Maximum Depth of Binary Tree--二叉树高度--递归或迭代--C++,Python解法

    题目地址:Maximum Depth of Binary Tree - LeetCode Given a binary tree, find its maximum depth. The maximu ...

  6. 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 l ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  9. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

最新文章

  1. 上帝的指纹——分形与混沌
  2. 宝塔面板php日志在哪里,宝塔面板怎么查看网站日志?
  3. Scala - 快速学习08 - 函数式编程:高阶函数
  4. C指针原理(27)-编译基本原理-语法树及其实现7
  5. Linux环境下搭建FTP服务器
  6. 突破极限–如何使用AeroGear Unified Push for Java EE和Node.js
  7. 输入数字存入数组C语言,//从键盘上输入若干整数,并将其存入数组中,并统计输入数据的个...
  8. 在Python当中如何打印输出当前时间(代码)
  9. 《c++ const 详细总结》--转载
  10. 人际沟通最忌讳一脸死相【转】
  11. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q136-Q138)
  12. Android--多线程之Handler
  13. 『总结』CSS/CSS3常用样式与web移动端资源
  14. 一些同样适用于人生的计算机原理
  15. Windows Server 2008 RemoteApp(五)---远程桌面Web访问
  16. ENVI软件图像放缩出现重影的解决办法
  17. python金山词霸单词本批量导入
  18. pinia - 大菠萝的使用
  19. Python爬取各种类型网站数据(视频,图片居多)
  20. 前端常用60个工具方法

热门文章

  1. DCMTK:演示状态的VR和IOD检查器
  2. VTK:小部件之CompassWidget
  3. VTK:Utilities之SortDataArray
  4. VTK:Medical之MedicalDemo2
  5. OpenCV反向项目功能用法的实例(附完整代码)
  6. OpenGL springmass弹簧质量模拟器的实例
  7. C语言圈排序Cycle Sort算法(附完整源码)
  8. C++实现堆排序(附完整源码)
  9. QT的QWhatsThis类的使用
  10. QT的QVarLengthArray类的使用