Re: [闲聊] 每日leetcode

楼主: abcd991276 (QQ)   2024-02-27 12:21:30
543. Diameter of Binary Tree
题目要算树最远两个节点的间隔
我就烂递回算树左右高然后加起来
再递回把整个树的节点都算一次
超慢速才赢6.96%
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
def Tree_height(root):
if root is not None:
return max(Tree_height(root.left), Tree_height(root.right)) +
1
else:
return 0
if root is not None:
path_length = Tree_height(root.left) + Tree_height(root.right)
return max(path_length, self.diameterOfBinaryTree(root.left),
self.diameterOfBinaryTree(root.right))
else:
return 0
作者: sustainer123 (caster)   2024-02-27 12:22:00
大师
作者: HccrtZ (Violet)   2024-02-27 12:22:00
剩我不懂树了
作者: DJYOSHITAKA (Evans)   2024-02-27 12:31:00
大师
作者: JIWP (JIWP)   2024-02-27 12:51:00
我就是那个6%

Links booklink

Contact Us: admin [ a t ] ucptt.com