zhy0216
2017-02-17 02:12:12 +08:00
我觉得是一样, 我改了下代码, 逻辑是一致的, 就是从数组->dict:
```python
def helper(self, root, sum):
from collections import defaultdict
if not root: return None
result = defaultdict(int)
result[root.val] += 1
left, right = self.helper(root.left, sum), self.helper(root.right, sum)
if left:
for key in left:
result[key+root.val] += left[key]
if right:
for key in right:
result[key+root.val] += right[key]
self.count += result[sum]
return result
```
不过不知怎么, 速度更慢了