Skip to content

Sum of paths in a binary tree #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: binary-tree-path-sum
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
* [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py)
* [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py)
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
* [Binary Tree Path Sum](data_structures/binary_tree/binary_tree_path_sum.py)
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
Expand Down
22 changes: 22 additions & 0 deletions data_structures/binary_tree/Binary tree path sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:

global result
result = 0

def dfs(node, target):
if node is None: return
find_path_from_node(node, target)
dfs(node.left, target)
dfs(node.right, target)

def find_path_from_node(node, target):
global result
if node is None: return
if node.val == target: result += 1
find_path_from_node(node.left, target-node.val)
find_path_from_node(node.right, target-node.val)

dfs(root, sum)

return result
78 changes: 78 additions & 0 deletions data_structures/binary_tree/binary_tree_path_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()