979. Distribute Coins in Binary Tree
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 77% Topics: Tree, Depth-First Search, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def distributeCoins(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def dfs(root, result):
if not root:
return 0
left, right = dfs(root.left, result), dfs(root.right, result)
result[0] += abs(left) + abs(right)
return root.val + left + right - 1
result = [0]
dfs(root, result)
return result[0]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions