-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path129.sum-root-to-leaf-numbers.py
65 lines (49 loc) · 1.65 KB
/
129.sum-root-to-leaf-numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# @lc app=leetcode id=129 lang=python3
#
# [129] Sum Root to Leaf Numbers
#
# @lc code=start
# 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:
paths = []
def isLeaf(self, node):
if not node:
return False
return not (node.left or node.right)
def constructPaths(self, partial, node):
"""Generates all paths and prints it to the console
If the Node is None, we've hit a dead end so return back
1
/ \
2
We're always looking out for the 'leaf' nodes
Check if the node is leaf. If the node is leaf, add to the self.paths list
Otherwise keep generating paths in left or right
Args:
partial (list): a partially constructed path (potentially)
node (TreeNode | None): a tree node or none
"""
if not node:
return
partial = partial + [node.val]
if self.isLeaf(node):
# print("Found path ", partial)
num = "".join([str(i) for i in partial])
self.paths.append(int(num))
else:
self.constructPaths([i for i in partial], node.left)
self.constructPaths([i for i in partial], node.right)
def sumNumbers(self, root: TreeNode) -> int:
if not root:
return 0
self.paths = [] # this must be resest across leetcode answers :(
self.constructPaths([], root)
print(self.paths)
return sum(self.paths)
# @lc code=end