文章目录

  • 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阿明),一起加油、一起学习进步!

LeetCode 1522. Diameter of N-Ary Tree(递归)相关推荐

  1. Leetcode 1522. Diameter of N-Ary Tree [Python]

    对于普通二叉树的diameter. 543. Diameter of Binary Tree 543. https://leetcode.com/problems/diameter-of-binary ...

  2. 【同124】LeetCode 543. Diameter of Binary Tree

    LeetCode 543. Diameter of Binary Tree Solution1:我的答案 思路和方法就是同124题的 /*** Definition for a binary tree ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. [leetcode]623. Add One Row to Tree

    [leetcode]623. Add One Row to Tree Analysis 今天要吃柚子-- [每天刷题并不难0.0] Given the root of a binary tree, t ...

  5. LeetCode Unique Binary Search Trees II(递归或者dp)

    问题:给出一个正整数,要求构造由1到n组成的所有的二叉搜索树 思路: 1.递归法 在求从1到n的二叉搜索树时,枚举中间值i(1到n),求[1,i-1]和[i+1,n]分别构成的二叉搜索树,然后根据左右 ...

  6. source tree 递归子模块_每日刷题3--漫谈二叉树的递归遍历

    leetcode 特点1 虽然是从root开始,但是 严重依赖从下到上的反馈的数据 ,例如求tree的高度 题目1 最近公共祖先(LCA) 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百 ...

  7. leetcode python3 简单题101. Symmetric Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...

  8. leetcode python3 简单题100. Same Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百题 (1)题目 英文: Given two binary trees, wri ...

  9. 【LeetCode】513. Find Bottom Left Tree Value

    问题描述 问题链接:https://leetcode.com/problems/find-bottom-left-tree-value/#/description Given a binary tre ...

最新文章

  1. xp 5gt ***
  2. linux shell 的 for 循环
  3. 前端学习(1042):todoList存储
  4. 编程语言c 是什么意思,C/C++知识点之C语言中%*s,%*c 是什么意思
  5. java 三级菜单栏的添加_[Java教程]jquery实现的三级导航菜单实例代码
  6. 年轻人的“第一次”寄快递?上有产品下有电商的小米注册快递商标
  7. matlab基数排序,如何在MATLAB中编写基数排序的程序
  8. mysqlslap 压力测试工具
  9. click事件的执行顺序
  10. iOS开发进阶-实现多线程的3种方法
  11. (2) python 使用pd.read_excel 读取excel时,选取某一列为索引
  12. go语言练习:条件语句和循环语句
  13. Hadoop初入门的坑
  14. CISCO 基于时间访问控制
  15. 关于cosine_similarity参数的问题
  16. [CTSC2016]时空旅行(线段树+凸包)
  17. 驱动人生解决电脑总是无端卡死/黑屏需强制关机的现象
  18. 计算机动画技术的应用领域,3D动画技术的应用领域
  19. Bonobo Git Server的使用
  20. 企业应用大数据开源框架的意义何在?

热门文章

  1. Django的核心思想ORM
  2. 综合素质计算机考点,教师资格证小学综合素质考点及考试真题:信息处理能力...
  3. java final修饰属性_Java final关键字用来修饰类、方法、属性
  4. STM32串口通信中使用printf发送数据配置方法 开发环境 Keil
  5. 2017.12.20-21
  6. margin 负边距应用
  7. angularjs控制器之间的数据共享与通信
  8. thinkpad s3 安装win8 kali双系统笔记
  9. 详细解析Java中抽象类和接口的区别(很容易理解错)
  10. Be My Eyes app:我是你的眼