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