面试做错的题目汇总下

二叉树根节点到叶子节点的所有路径和

https://www.nowcoder.com/practice/185a87cd29eb42049132aed873273e83?tpId=46&tqId=29051&tPage=1&rp=1&ru=%2Fta%2Fleetcode&qru=%2Fta%2Fleetcode%2Fquestion-ranking&tab=answerKey

1
2
3
4
5
6
7
8
class Solution(object):
def sumNumbers(self, root, prefix=0):
if root == None:
return 0
prefix = prefix * 10 + root.val
if root.left == None and root.right == None:
return prefix
return self.sumNumbers(root.left, prefix) + self.sumNumbers(root.right, prefix)