Re: [閒聊] 每日leetcode

看板Marginalman作者 (QQ)時間2月前 (2024/02/27 12:21), 編輯推噓3(301)
留言4則, 4人參與, 2月前最新討論串3/228 (看更多)
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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.121.38.49 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709007702.A.5AE.html

02/27 12:22, 2月前 , 1F
大師
02/27 12:22, 1F

02/27 12:22, 2月前 , 2F
剩我不懂樹了
02/27 12:22, 2F

02/27 12:31, 2月前 , 3F
大師
02/27 12:31, 3F

02/27 12:51, 2月前 , 4F
我就是那個6%
02/27 12:51, 4F
文章代碼(AID): #1btMDMMk (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1btMDMMk (Marginalman)