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 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:
- The depth of the tree is at most 1000.
- 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相关推荐
- 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 ...
- 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 ...
- 【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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- leetcode python3 简单题104. Maximum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...
最新文章
- 上帝的指纹——分形与混沌
- 宝塔面板php日志在哪里,宝塔面板怎么查看网站日志?
- Scala - 快速学习08 - 函数式编程:高阶函数
- C指针原理(27)-编译基本原理-语法树及其实现7
- Linux环境下搭建FTP服务器
- 突破极限–如何使用AeroGear Unified Push for Java EE和Node.js
- 输入数字存入数组C语言,//从键盘上输入若干整数,并将其存入数组中,并统计输入数据的个...
- 在Python当中如何打印输出当前时间(代码)
- 《c++ const 详细总结》--转载
- 人际沟通最忌讳一脸死相【转】
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q136-Q138)
- Android--多线程之Handler
- 『总结』CSS/CSS3常用样式与web移动端资源
- 一些同样适用于人生的计算机原理
- Windows Server 2008 RemoteApp(五)---远程桌面Web访问
- ENVI软件图像放缩出现重影的解决办法
- python金山词霸单词本批量导入
- pinia - 大菠萝的使用
- Python爬取各种类型网站数据(视频,图片居多)
- 前端常用60个工具方法