MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / count_coins

Function count_coins

data_structures/binary_tree/distribute_coins.py:92–100  ·  view source on GitHub ↗

>>> count_coins(None) 0

(node: TreeNode | None)

Source from the content-addressed store, hash-verified

90 return count_nodes(node.left) + count_nodes(node.right) + 1
91
92 def count_coins(node: TreeNode | None) -> int:
93 """
94 >>> count_coins(None)
95 0
96 """
97 if node is None:
98 return 0
99
100 return count_coins(node.left) + count_coins(node.right) + node.data
101
102 if count_nodes(root) != count_coins(root):
103 raise ValueError("The nodes number should be same as the number of coins")

Callers 1

distribute_coinsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected