LeetCode 559. Maximum Depth of N-ary Tree(N-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.
/*
// 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的深度)的更多相关文章
- (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 ...
- 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 ...
- 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 ...
- [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 ...
- [leetcode] 559. Maximum Depth of N-ary Tree (easy)
原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...
- 559. Maximum Depth of N-ary Tree - LeetCode
Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...
- 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 ...
- 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 ...
随机推荐
- Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享
Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.实现用户家目录的http共享前提 在配置家目录共 ...
- 黄金矿工(LeetCode Medium难度)1129题 题解(DFS)
题目描述: 给定一个二维网络,给定任意起点与终点.每一步可以往4个方向走.要找出黄金最多的一条线路. 很明显的是要“一条路走到黑,一直下去直到某个条件停止”. 运用dfs(深度优先搜索)求解. 因为起 ...
- dart 中的 try on catch
catch 捕获异常 捕获了一个异常后,就停止了捕获异常过程.捕获一个异常,你就有机会去处理它: try { breedMoreLlamas(); } on OutOfLlamasException ...
- Ruby for
#!/usr/bin/ruby -w# -*- coding: UTF-8 -*-for i in 1..5 print i," "endprint "\n"f ...
- template_DefaultType
#include <iostream> using namespace std; template <typename T1,typename T2=int> class Te ...
- js里apply用法
1.Function.apply,用于构造函数的继承,继承另外一个构建对象的属性与方法 function People(name,age){ this.name = name; thi ...
- Java-Modifier类常用方法详解
一.Modifier类的定义 Modifier类 (修饰符工具类) 位于 java.lang.reflect 包中,用于判断和获取某个类.变量或方法的修饰符Modifier类将各个修饰符表示为相对应的 ...
- day008-python内置函数
一.ptthon内置函数 二.内置函数详细概述 2.1 abs(x):函数返回数字的绝对值. 注意: 1)x -- 数值表达式,可以是整数,浮点数,复数. 2)如果参数是一个复数,则返回它的大小. ...
- 彻底掌握网络通信(七)ConnectionReuseStrategy,ConnectionKeepAliveStrategy解析
网络通信系列文章序 彻底掌握网络通信(一)Http协议基础知识 彻底掌握网络通信(二)Apache的HttpClient基础知识 彻底掌握网络通信(三)Android源码中HttpClient的在不同 ...
- ent facebook 开源的golang orm 框架
ent 是facebook 开源的golang orm 框架,简单强大,具有提下特性 schema 即代码 方便的图遍历 静态类型以及显示api 多种存储引擎支持(当前是mysql,sqlite,以及 ...