From 88766e9291e000292e821458c722d6e3fa9b02ef Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Thu, 13 Oct 2022 21:01:24 +0100 Subject: [PATCH 1/3] feat: Binary tree path sum (#7135) --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2786e1f82de8..2b30539c5de0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) From 7303f81aae7e50e58b6216bf29c7db33a642ea4c Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Thu, 13 Oct 2022 21:01:58 +0100 Subject: [PATCH 2/3] feat: Basic binary tree implementation (#7135) --- .../binary_tree/binary_tree_path_sum.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 data_structures/binary_tree/binary_tree_path_sum.py diff --git a/data_structures/binary_tree/binary_tree_path_sum.py b/data_structures/binary_tree/binary_tree_path_sum.py new file mode 100644 index 000000000000..134289e32f4f --- /dev/null +++ b/data_structures/binary_tree/binary_tree_path_sum.py @@ -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() From 7f78d7337b1b1b7f2b83dff3095ea769cd371ed0 Mon Sep 17 00:00:00 2001 From: Aniket Mishra <70846580+Aniketmishra0@users.noreply.github.com> Date: Fri, 14 Oct 2022 18:56:11 +0530 Subject: [PATCH 3/3] Sum of paths in a binary tree --- .../binary_tree/Binary tree path sum | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 data_structures/binary_tree/Binary tree path sum diff --git a/data_structures/binary_tree/Binary tree path sum b/data_structures/binary_tree/Binary tree path sum new file mode 100644 index 000000000000..5abd95bb18a6 --- /dev/null +++ b/data_structures/binary_tree/Binary tree path sum @@ -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