A short Python solution


  • 0
    G
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return an integer
        # 1:47
        def sumNumbers(self, root, path = 0):
            if not root:
                return path
    
            path = path * 10 + root.val
            left = self.sumNumbers(root.left, path)
            right = self.sumNumbers(root.right, path)
    
            if left == path:
                return right
            elif right == path:
                return left
            else:
                return left + right

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.