Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 1.51 KB

_129. Sum Root to Leaf Numbers.md

File metadata and controls

57 lines (43 loc) · 1.51 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 07, 2024

Last updated : July 01, 2024


Related Topics : Tree, Depth-First Search, Binary Tree

Acceptance Rate : 67.95 %


Solutions

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumNumbers(self, root: Optional[TreeNode]) -> int:
        self.summ = 0

        def helper(current: Optional[TreeNode], currSum: []) -> None :
            if not current :
                print(int(''.join(currSum)))
                self.summ += int(''.join(currSum))
                return

            currSum.append(str(current.val))
            if current.left and current.right :
                helper(current.left, currSum)
                helper(current.right, currSum)
            elif current.left :
                helper(current.left, currSum)
            else :
                helper(current.right, currSum)

            currSum.pop()

        helper(root, [])
        return self.summ