标签:tree __init__ root etc python class ble col code
搞不定这种递归计算,可能我的头脑是“线性”的,这种一层一层的,想起来太费劲了,想的头发都没了。以后希望能有AI来写这种程序吧,AI不怕掉头发!
1 class Solution(object): 2 def __init__(self): 3 self.res = 0 4 5 def dfs(self,root): 6 if not root: 7 return 0 8 left = self.dfs(root.left) 9 right = self.dfs(root.right) 10 self.res += abs(left) + abs(right) 11 return root.val + left + right - 1 12 13 def distributeCoins(self, root: TreeNode) -> int: 14 self.dfs(root) 15 return self.res
标签:tree __init__ root etc python class ble col code
原文地址:https://www.cnblogs.com/asenyang/p/10758077.html