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.

/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if (root == NULL) {
return ;
} int maxd = ;
int tmp = ;
for (auto child : root->children)
{
tmp = + maxDepth(child);
if (tmp > maxd) {
maxd = tmp;
}
} return maxd;
} // int maxDepth(Node* root) {
// if (root == nullptr) return 0;
// int depth = 0;
// for (auto child : root->children) depth = max(depth, maxDepth(child));
// return 1 + depth;
// }
};

LeetCode 559. Maximum Depth of N-ary Tree(N-Tree的深度)的更多相关文章

  1. (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 ...

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

  3. 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 ...

  4. [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 ...

  5. [leetcode] 559. Maximum Depth of N-ary Tree (easy)

    原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...

  6. 559. Maximum Depth of N-ary Tree - LeetCode

    Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...

  7. 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. [LeetCode&Python] Problem 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 ...

  9. 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 ...

随机推荐

  1. 【转】大众点评CAT开源监控系统剖析

    https://www.cnblogs.com/yeahwell/p/cat.html 参考文档: 大众点评的实时监控系统分析(一) CAT_source_analyze 透过CAT,来看分布式实时监 ...

  2. 个人推荐的Java邮件配置

    # 大部分host都是smtp.加上你邮箱@后面的域名spring.mail.host=smtp.localhost.com spring.mail.username= spring.mail.pas ...

  3. PAT甲级1015题解——令人迷茫的翻译

    题目分析: 本题计算过程简单,但翻译令我迷茫:题意读清楚很重要(反正我是懵逼了)对于一个10进制的数,如果它是一个素数,把它转换成d进制,再将这个序列逆序排,这个逆序的d进制数的10进制表示如果也是素 ...

  4. Linux 下 svn 多个项目多用户分配

    安装步骤如下: 1.yum install subversion 2.输入rpm -ql subversion查看安装位置,如下图: 输入 svn –help可以查看svn的使用方法 需求 开发服务器 ...

  5. Helm 安装部署Kubernetes的dashboard

    Kubernetes Dashboard 是 k8s集群的一个 WEB UI管理工具,代码托管在 github 上,地址:https://github.com/kubernetes/dashboard ...

  6. Objective-C 消息发送与转发机制原理(摘)

    八面玲珑的 objc_msgSend 此函数是消息发送必经之路,但只要一提 objc_msgSend,都会说它的伪代码如下或类似的逻辑,反正就是获取 IMP 并调用: id objc_msgSend( ...

  7. graphql-compose graphql schema 生成工具集

    graphql-compose 是一个强大的graphql schema 生成工具集 包含以下特性 快速便捷的复杂类型生成 类型仓库,类型可以存储在schemacomposer 存储中 包含flowt ...

  8. 3-开发共享版APP(搭建指南)-修改手机验证码

    https://www.cnblogs.com/yangfengwu/p/11273743.html 请先看数据篇 或者参考 https://www.cnblogs.com/yangfengwu/p/ ...

  9. 19 条效率至少提高 3 倍的 MySQL 技巧

    阅读本文大概需要 4 分钟. 来源:https://zhuanlan.zhihu.com/p/49888088 本文我们来谈谈项目中常用的 MySQL 优化方法,共 19 条,具体如下: 1.EXPL ...

  10. pyqt(day3)

    一.在pycharm中配置qtdesigner C:\Python\Python37\Lib\site-packages\pyqt5_tools\designer.exe 二.ui文件转换成pytho ...