(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 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
.
--------------------------------------------------------------------------------------------------------------------------------------
这个和求二叉树的最大深度类似,只要掌握了二叉树的最大深度的解法以及理解了N叉树的原理,解决这个题就会很简单了。
递归/DFS:
C++代码:
- /*
- // 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) return ;
- int maxSum = ; //这个是必须的,不能写错。因为根节点的深度为1。
- for(Node *cur:root->children){
- maxSum = max(maxSum, + maxDepth(cur));
- }
- return maxSum;
- }
- };
BFS(迭代):
C++代码:
- /*
- // 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) return ;
- queue<Node*> q;
- q.push(root);
- int sum = ;
- while(!q.empty()){
- sum++;
- for(int i = q.size(); i > ; i--){
- auto t = q.front();
- q.pop();
- for(Node *cur : t->children){
- q.push(cur);
- }
- }
- }
- return sum;
- }
- };
(N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree的更多相关文章
- [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 解题报告
题目要求 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(N-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)
原题链接 思路: 简单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实现: / ...
- (二叉树 BFS DFS) 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 ...
- 【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 ...
随机推荐
- 第四次上机,ASP组件的使用
<html> <body> <% '以下连接数据库,建立一个Connection对象实例conn Set conn=Server.CreateObject("A ...
- Oracle字符到数值转换错误
[错误] [问题分析] line 3: 定义 NUM_VAL varchar2(500); line 9: NUM_VAL := 'NUM'+1; NUM_VAL是一个varchar类型的数据,而在数 ...
- SQL Server服务没有自动启动原因案例分析
这个案例是前两天出现的,一直没有时间总结,25号凌晨4点去处理数据库的故障问题.远程连上公司的局域网,psping检查发现服务器的1433端口不通,数据库连接不上,但是主机又能ping通,登录服务器检 ...
- 从0开始的Python学习011模块
简介 你已经学习了如何在你的程序中定义一次函数而重用代码.如果你想要在其他程序中重用很多函数,那么你该如何编写程序呢?你可能已经猜到了,答案是使用模块.模块基本上就是一个包含了所有你定义的函数和变量的 ...
- Postgres中文分词
环境 CentOS Linux release 7.2.1511 (Core) 安装Postgres 安装postgres很简单 yum安装 sudo yum install postgresql-s ...
- PowerShell或命令行运行javac xx.java提示“编码GBK的不可映射字符”
由于JDK是国际版的,我们在用javac编译时,编译程序首先会获得我们操作系统默认采用的编码格式(GBK),然后JDK就把Java源文件从GBK编码格式转换为Java内部默认的Unicode格式放入内 ...
- LeetCode算法题-Largest Number At Least Twice of Others(Java实现)
这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...
- Storm入门(二)集群环境安装
1.集群规划 storm版本的变更:storm0.9.x storm0.10.x storm1.x上面这些版本里面storm的核心源码是由Java+clojule组成的.storm2.x后期这个 ...
- CMake与Make最简单直接的区别 [转]
写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时就会特别麻烦 ...
- 软件工程作业 - word count
(编程和软件工程作业系列) 实践最简单的项目:WC 实践是理论的基础和验证标准,希望读者贯彻“做中学”的思想,动手实现下面的项目,并和别人的成绩相比较,分析产生差距的原因. 1. 实现一个简单而完整的 ...