LeetCode 1522. Diameter of N-Ary Tree(递归)

news/2024/11/29 8:02:33/

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

Given a root of an N-ary tree, you need to compute the length of the diameter of the tree.

The diameter of an N-ary tree is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root.

(Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value.)

Example 1:
在这里插入图片描述

Input: root = [1,null,3,2,4,null,5,6]
Output: 3
Explanation: Diameter is shown in red color.

Example 2:
在这里插入图片描述

Input: root = [1,null,2,null,3,4,null,5,null,6]
Output: 4

Example 3:
在这里插入图片描述

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: 7Constraints:
The depth of the n-ary tree is less than or equal to 1000.
The total number of nodes is between [0, 10^4].

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/diameter-of-n-ary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*/class Solution {int ans = 0;
public:int diameter(Node* root) {h(root);return ans;}int h(Node* root){if(!root) return 0;int maxdep1 = 0, maxdep2 = 0, height;for(auto c : root->children){height = h(c);if(height >= maxdep1){maxdep2 = maxdep1;maxdep1 = height;}else if(height > maxdep2)maxdep2 = height;}ans = max(ans, maxdep2+maxdep1);return max(maxdep1, maxdep2) + 1;}
};

32 ms 10.7 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明


http://www.ppmy.cn/news/280925.html

相关文章

以太网帧长度1518、1522、1536的说明

一、IEEE802.3 以太网帧结构&#xff1a; Preamble (7-bytes) &#xff0d;&#xff0d;前导码 Start Frame Delimiter (1-byte) &#xff0d;&#xff0d;定界符 Dest. MAC Address (6-bytes) &#xff0d;&#xff0d;目的地址 Source MAC Address (6-bytes) &#xf…

Leetcode 1522. Diameter of N-Ary Tree (python+cpp)

Leetcode 1522. Diameter of N-Ary Tree 题目&#xff1a;解法&#xff1a; 题目&#xff1a; 解法&#xff1a; 这是543的follow up&#xff0c;解法几乎是一样的&#xff0c;只不过左子树和右子树的深度替换成所有子树里面深度最大的两个 class Solution:def diameter(self…

洛谷P1522牛的旅行——floyd

题目&#xff1a;https://www.luogu.org/problemnew/show/P1522 懒于仔细分情况而直接像题解那样写floyd然后不明白最后一步max的含义了... 分开考虑怎么保证在一个内呢&#xff1f;如果新连边的min与原直径的max在三个连通块里怎么办&#xff1f; 代码如下&#xff1a; #inclu…

洛谷p-1522又是Floyd

挺简单一个题&#xff0c;可惜当时没想到&#xff0c;有点巧妙丫&#xff01; #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #define maxn 255 using namespace std; char list[maxn][maxn]; do…

luogu P1522 牛的旅行 Cow Tours

题目传送门&#xff1a;https://www.luogu.org/problemnew/show/P1522 题意&#xff1a; 给出n个坐标&#xff0c;以及他们的连通情况&#xff0c;你可以再选任意两个点相连&#xff0c;求此时的最小的直径&#xff08;图的直径&#xff1a;图中最远两点的距离&#xff09;。 …

hdu 1522

这题是一个匹配问题&#xff0c;一开始想用km做最佳匹配&#xff0c;但是图很大肯定会tle&#xff0c;思考无果后只能去搜搜题解&#xff0c;这是个经典问题吧。从http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html这里找到了相应的资料。 稳定婚姻是组合数学…

P1522 牛的旅行

这题挺好……有几个坑……&#xff08;反正我都跳进去了&#xff09; 对于新的更大的图&#xff0c;由于求的是最小连接边&#xff0c;所以它的值可能小于之前单独一个图的最长的最短路…… 所以之后的值应该取个max&#xff08;emmm……&#xff09; 所以第一次我只拿了70。。…