【发布时间】:2015-06-05 08:10:51
【问题描述】:
我有一个二叉搜索树,其中每个节点代表一个游戏长度。我必须返回一个字典,其中键是游戏的长度,值是具有该长度的游戏数。递归调用遍历树中的每个节点,但它返回一个不正确的字典。我很肯定问题在于我如何返回字典。任何帮助将不胜感激
game_len = {}
if not node.children:
key = len(node.possible_next_moves())
if key not in game_len:
game_len[key] = 1
else:
game_len[key] += 1
else:
key = len(node.possible_next_moves())
if key not in game_len:
game_len[key] = 1
else:
game_len[key] += 1
[game_lengths(child) for child in node.children]
return game_len
【问题讨论】:
-
您能否补充一下您的代码如何不起作用的问题?
标签: python dictionary recursion binary-tree