leetcode 129. 求根到叶子节点数字之和 发表于 2018-12-20 | 分类于 leetcode 题目链接:leetcode 思路:广度优先 123456789101112131415161718192021222324252627# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Nonefrom collections import dequeclass Solution: def sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ if not root:return 0 res = 0 q = deque() q.append((root,root.val)) while q: node,val = q.popleft() #leaf node if not node.left and not node.right: res += val if node.left: q.append((node.left,node.left.val+val*10)) if node.right: q.append((node.right,node.right.val+val*10)) return res