forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinary_tree_path_sum.py
78 lines (57 loc) · 1.59 KB
/
binary_tree_path_sum.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Given the root of a binary tree and an integer target,
find the number of paths where the sum of the values
along the path equals target.
Leetcode reference: https://leetcode.com/problems/path-sum-iii/
"""
from __future__ import annotations
class Node:
"""
A Node has data variable and pointers to Nodes to its left and right.
"""
def __init__(self, data: int) -> None:
self.data = data
self.left: Node | None = None
self.right: Node | None = None
class BinaryTreePathSum:
r"""
The below tree looks like this
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
>>> tree = Node(10)
>>> tree.left = Node(5)
>>> tree.right = Node(-3)
>>> tree.left.left = Node(3)
>>> tree.left.right = Node(2)
>>> tree.right.right = Node(11)
>>> tree.left.left.left = Node(3)
>>> tree.left.left.right = Node(-2)
>>> tree.left.right.right = Node(1)
>>> BinaryTreePathSum(tree).path_sum(8)
3
>>> BinaryTreePathSum(tree).path_sum(7)
1
>>> tree.right.right = Node(10)
>>> BinaryTreePathSum(tree).path_sum(8)
2
"""
def __init__(self, tree: Node) -> None:
self.tree = tree
def depth_first_search(self, node: Node | None) -> int:
"""
"""
if node is None:
return 0
return self.depth_first_search(node)
def path_sum(self, target: int) -> int:
"""
"""
return self.depth_first_search(self.tree, target)
if __name__ == "__main__":
import doctest
doctest.testmod()