class Solution: def sumNumbers(self, root: TreeNode) -> int: def sums(root,pre): if not root: return 0 total=pre*10+root.val if not root.left and not root.right: return total else: return sums(root.left,total)+sums(root.right,total) return sums(root,0)
简单递归,深度优先搜索