LeetCode 129 Sum Root to Leaf Numbers

LeetCode 129 Sum Root to Leaf Numbers Problem

Download Code
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def sumNumbers(self, root):
        :type root: TreeNode
        :rtype: int
        if root is None:
            return 0
        res = 0
        # bfs with queue
        queue = [(root, root.val)]
        while len(queue) > 0:
            curr, curr_value = queue.pop(0)
            if curr.left is None and curr.right is None:
                res += curr_value
            if curr.left:
                queue.append((curr.left, curr_value * 10 + curr.left.val))
            if curr.right:
                queue.append((curr.right, curr_value * 10 + curr.right.val))
        return res
Download Sum Root to Leaf Numbers.py

List of all Sum Root to Leaf Numbers problems

Leetcode 129 Sum Root to Leaf Numbers problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

self.val = x

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.