Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (franchouchouISBEST)時間1年前 (2024/05/19 21:56), 編輯推噓3(301)
留言4則, 4人參與, 1年前最新討論串245/1554 (看更多)
補一下昨天的:( Distribute Coins in Binary Tree 其實還蠻不好想的 想了一陣子才想說可以往上回傳 "我要往上送多少金幣or我需要從上面收下多少金幣" 然後dfs的過程中 把往上送的跟從上面收下的都全部加起來就是答案了 有趣的一題 (如果沒想出來就是糞肏的一題了) def __init__(self): self.ans = 0 def dfs(self, root: Optional[TreeNode]) -> int: if root is None: return 0 left = self.dfs(root.left) right = self.dfs(root.right) self.ans = self.ans + abs(left) + abs(right) return root.val + (left+right) - 1 def distributeCoins(self, root: Optional[TreeNode]) -> int: self.dfs(root) return self.ans -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.227.99 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1716127001.A.3CF.html

05/19 21:57, 1年前 , 1F
別捲了
05/19 21:57, 1F

05/19 21:58, 1年前 , 2F
我不看解答根本想不到這個思維
05/19 21:58, 2F

05/19 22:01, 1年前 , 3F
今天怎麼是個hard:(
05/19 22:01, 3F

05/19 22:11, 1年前 , 4F
Hard直接開擺 不過看答案好像差一點就想出來了
05/19 22:11, 4F
文章代碼(AID): #1cIWKPFF (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cIWKPFF (Marginalman)