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

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:self.res = 0if not root:return self.resself.getdia(root)return self.resdef getdia(self, root):if not root:return 0left = self.getdia(root.left)right = self.getdia(root.right)self.res = max(self.res, left + right)return 1 + max(left, right)

换到这题,多了很多子树,那其实思路一样,只要维护全局最大值,并且选择最长的两个子树的路径长度加起来就好。用heap实现;注意判断heap里有没有东西。

"""
# Definition for a Node.
class Node:def __init__(self, val=None, children=None):self.val = valself.children = children if children is not None else []
"""class Solution:def diameter(self, root: 'Node') -> int:""":type root: 'Node':rtype: int"""if not root:return 0self.res = float('-inf')self.getdia(root)return self.resdef getdia(self, root):if not root:return 0heap = []for child in root.children:temp = self.getdia(child)heapq.heappush(heap, -temp)first = 0second = 0if heap:first = -1*heapq.heappop(heap)if heap: second = -1*heapq.heappop(heap)self.res = max(self.res, first + second)return 1 + first

Leetcode 1522. Diameter of N-Ary Tree [Python]相关推荐

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

    文章目录 1. 题目 2. 解题 1. 题目 Given a root of an N-ary tree, you need to compute the length of the diameter ...

  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每日一题·买卖股票问题(Python)

    leetcode每日一题·买卖股票问题(Python) 买卖股票的最佳时机(股票最大利润) 题目链接 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的 ...

  5. LeetCode刷题——哈希表(python语言)

    LeetCode刷题--哈希表(python语言) 一.哈希表 1.1 哈希表的概念 哈希表,也叫散列表.其实可以很像python的字典,也就是键(key)值(Hash(key))对,最简单也最常用的 ...

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

  7. leetcode(226)—— Invert Binary Tree(Python/C++)

    Invert Binary Tree 二叉树节点定义: struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) ...

  8. Leetcode May Challenge - 05/07: Cousins in Binary Tree(Python)

    题目描述 In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k ...

  9. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

最新文章

  1. 250相当于什么显卡_GTX1660Ti显卡搭配知识:GTX1660Ti配什么CPU和主板?
  2. java学习笔记12--异常处理
  3. Linux环境下gcc编译链接库-lz -lrt -lm -lc都是什么库?
  4. 超级计算机预测2月有雪寒潮,神预测!中国超级计算提前半个月预测了美国的寒潮...
  5. 生日小助手的问答帮助——随时更新,长期有效……
  6. API网关—Spring Cloud Zuul
  7. python异常处理与导入模块与导入包
  8. Gurobi运筹学开发教程04:拉格朗日分解技术及其实现
  9. 台式网卡计算机,台式机万能网卡驱动,教您如何给台式机安装万能网卡驱动
  10. 【毕业设计】深度学习疲劳检测 驾驶行为检测 - python opencv cnn
  11. 读《华为“打工皇帝”徐家骏的十年感悟》的心志提升
  12. 2018中南大学 计算机考研分数,中南大学2018年硕士研究生招生复试基本分数线
  13. verilog从txt中读取_verilog语言中的文件读写
  14. python命名规则数字开头的成语_day01 Python基础
  15. Business Cycle 【UVALive - 7501】【二分答案+思维处理】
  16. 使用Pytorch实现简单的LSTM股票预测框架
  17. linux开机自启jar包
  18. 夏日狂欢 — BODY JAZZ两周年庆典———若水,以柔见世界
  19. 路遥短篇小说之《匆匆过客》
  20. 正弦波逆变器c语言程序源码,官方开源-EG8010单相纯正弦波逆变器驱动板资料分享...

热门文章

  1. LSTM-代码讲解(股票预测)
  2. 前端必备技能之----节流
  3. 关于用户成长体系,一份不能错过的笔记
  4. 用python写一个密码生成器函数
  5. php图马,关于File Upload的一些思考
  6. CocosCreator2.4 物体跟随鼠标或触摸点
  7. 吉大软件专硕896-2008
  8. ntpdate同步更新时间
  9. Mysql截取字符串(含指定字符串)
  10. TextView文本大小自动适配与TextView边距的去除