From 7c4ddc633cb39d03c4f661f9d149b7766cbbc9fb Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:38:43 -0400 Subject: [PATCH 1/7] test: Add unit tests --- .../binary_tree/binary_tree_path_sum.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/data_structures/binary_tree/binary_tree_path_sum.py b/data_structures/binary_tree/binary_tree_path_sum.py index a3fe9ca7a7e2..90205b593cc5 100644 --- a/data_structures/binary_tree/binary_tree_path_sum.py +++ b/data_structures/binary_tree/binary_tree_path_sum.py @@ -50,6 +50,26 @@ class BinaryTreePathSum: >>> tree.right.right = Node(10) >>> BinaryTreePathSum().path_sum(tree, 8) 2 + >>> BinaryTreePathSum().path_sum(None, 0) + 0 + >>> BinaryTreePathSum().path_sum(tree, 0) + 0 + + The second tree looks like this + + >>> tree2 = Node(0) + >>> tree2.left = Node(5) + >>> tree2.right = Node(5) + 0 + / \ + 5 5 + >>> BinaryTreePathSum().path_sum(tree, 5) + 2 + >>> BinaryTreePathSum().path_sum(tree, -1) + 0 + >>> BinaryTreePathSum().path_sum(tree, 0) + 1 + """ target: int From 1ee8199cd3c529bc72300552df95e54fffc5fa8d Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:43:10 -0400 Subject: [PATCH 2/7] test: Add successful tests in binaree_tree_path_sum --- .../binary_tree/binary_tree_path_sum.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_path_sum.py b/data_structures/binary_tree/binary_tree_path_sum.py index 90205b593cc5..43df30f2e308 100644 --- a/data_structures/binary_tree/binary_tree_path_sum.py +++ b/data_structures/binary_tree/binary_tree_path_sum.py @@ -56,18 +56,19 @@ class BinaryTreePathSum: 0 The second tree looks like this - - >>> tree2 = Node(0) - >>> tree2.left = Node(5) - >>> tree2.right = Node(5) 0 / \ 5 5 - >>> BinaryTreePathSum().path_sum(tree, 5) + + >>> tree2 = Node(0) + >>> tree2.left = Node(5) + >>> tree2.right = Node(15) + + >>> BinaryTreePathSum().path_sum(tree2, 5) 2 - >>> BinaryTreePathSum().path_sum(tree, -1) + >>> BinaryTreePathSum().path_sum(tree2, -1) 0 - >>> BinaryTreePathSum().path_sum(tree, 0) + >>> BinaryTreePathSum().path_sum(tree2, 0) 1 """ From 8ea7d2a5857574253478a6eb6624df09a98d8471 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:48:25 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/binary_tree_path_sum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_path_sum.py b/data_structures/binary_tree/binary_tree_path_sum.py index 43df30f2e308..d8e6f06123ee 100644 --- a/data_structures/binary_tree/binary_tree_path_sum.py +++ b/data_structures/binary_tree/binary_tree_path_sum.py @@ -54,7 +54,7 @@ class BinaryTreePathSum: 0 >>> BinaryTreePathSum().path_sum(tree, 0) 0 - + The second tree looks like this 0 / \ @@ -63,7 +63,7 @@ class BinaryTreePathSum: >>> tree2 = Node(0) >>> tree2.left = Node(5) >>> tree2.right = Node(15) - + >>> BinaryTreePathSum().path_sum(tree2, 5) 2 >>> BinaryTreePathSum().path_sum(tree2, -1) From 7ddec3b8b2c6ae7da30b1655e0e645e203b48be3 Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:08:19 -0400 Subject: [PATCH 4/7] create invert_binary_tree.py with class and start function --- .../binary_tree/invert_binary_tree.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 data_structures/binary_tree/invert_binary_tree.py diff --git a/data_structures/binary_tree/invert_binary_tree.py b/data_structures/binary_tree/invert_binary_tree.py new file mode 100644 index 000000000000..f105d71f37e2 --- /dev/null +++ b/data_structures/binary_tree/invert_binary_tree.py @@ -0,0 +1,31 @@ +""" +Given the root of a binary tree, invert the tree and return its root. + +Leetcode: https://leetcode.com/problems/invert-binary-tree/description/ + +If n is the number of nodes in the tree, then: +Time complexity: O(n) as every subtree needs to be mirrored, we visit each node once. + +Space complexity: O(h) where h is the height of the tree. This recursive algorithm +uses the space of the stack which can grow to the height of the binary tree. +The space complexity will be O(n log n) for a binary tree and O(n) for a skewed tree. +""" + +from __future__ import annotations +from dataclasses import dataclass + +@dataclass +class TreeNode: + """ + A TreeNode has a data variable and pointers to TreeNode objects for its left and right children. + """ + + def __init__(self, data: int) -> None: + self.data = data + self.left: TreeNode | None = None + self.right: TreeNode | None = None + + + @property + def mirror_binary_tree(self, root): + pass \ No newline at end of file From c159f6edb7c6f4c03df62eb4ad460b4e230cb809 Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:57:26 -0400 Subject: [PATCH 5/7] create preorder function --- .../binary_tree/invert_binary_tree.py | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/invert_binary_tree.py b/data_structures/binary_tree/invert_binary_tree.py index f105d71f37e2..0d2e5ccf9fbc 100644 --- a/data_structures/binary_tree/invert_binary_tree.py +++ b/data_structures/binary_tree/invert_binary_tree.py @@ -26,6 +26,59 @@ def __init__(self, data: int) -> None: self.right: TreeNode | None = None - @property - def mirror_binary_tree(self, root): - pass \ No newline at end of file +class MirrorBinaryTree: + def mirror_binary_tree(self, root : TreeNode): + """ + Invert a binary tree and return the new root. + + Returns the root of the mirrored binary tree. + + >>> tree = TreeNode(0) + >>> tree.left = TreeNode(10) + >>> tree.right = TreeNode(20) + >>> result_tree = MirrorBinaryTree().mirror_binary_tree(tree) + >>> print_preorder(result_tree) + 0 + 20 + 10 + """ + + if not root: + return None + + if root.left: + self.mirror_binary_tree(root.left) + + if root.right: + self.mirror_binary_tree(root.right) + + root.left, root.right = root.right, root.left + + return root + +def print_preorder(root: TreeNode | None) -> None: + """ + Print pre-order traversal of the tree . + + >>> root = TreeNode(1) + >>> root.left = TreeNode(2) + >>> root.right = TreeNode(3) + >>> print_preorder(root) + 1 + 2 + 3 + >>> print_preorder(root.right) + 3 + """ + if not root: + return None + if root: + print(root.data) + print_preorder(root.left) + print_preorder(root.right) + + +if __name__ == "__main__": + import doctest + doctest.testmod() + From 7d7e68ca5f86fe1a0138096b63acbfed88a6fcc1 Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:04:39 -0400 Subject: [PATCH 6/7] add test to invert_binary_tree --- data_structures/binary_tree/invert_binary_tree.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/binary_tree/invert_binary_tree.py b/data_structures/binary_tree/invert_binary_tree.py index 0d2e5ccf9fbc..58b346f625d5 100644 --- a/data_structures/binary_tree/invert_binary_tree.py +++ b/data_structures/binary_tree/invert_binary_tree.py @@ -41,6 +41,10 @@ def mirror_binary_tree(self, root : TreeNode): 0 20 10 + >>> tree2 = TreeNode(9) + >>> result_tree2 = MirrorBinaryTree().mirror_binary_tree(tree2) + >>> print_preorder(result_tree2) + 9 """ if not root: From 155d2455decc6f5a391e1d19c5d9e9c8da23f0dc Mon Sep 17 00:00:00 2001 From: Ronald Ngounou <74538524+ronaldngounou@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:06:21 -0400 Subject: [PATCH 7/7] implemented invert_binary_tree --- data_structures/binary_tree/invert_binary_tree.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/binary_tree/invert_binary_tree.py b/data_structures/binary_tree/invert_binary_tree.py index 58b346f625d5..1279521019d2 100644 --- a/data_structures/binary_tree/invert_binary_tree.py +++ b/data_structures/binary_tree/invert_binary_tree.py @@ -27,7 +27,7 @@ def __init__(self, data: int) -> None: class MirrorBinaryTree: - def mirror_binary_tree(self, root : TreeNode): + def invert_binary_tree(self, root : TreeNode): """ Invert a binary tree and return the new root. @@ -36,13 +36,13 @@ def mirror_binary_tree(self, root : TreeNode): >>> tree = TreeNode(0) >>> tree.left = TreeNode(10) >>> tree.right = TreeNode(20) - >>> result_tree = MirrorBinaryTree().mirror_binary_tree(tree) + >>> result_tree = MirrorBinaryTree().invert_binary_tree(tree) >>> print_preorder(result_tree) 0 20 10 >>> tree2 = TreeNode(9) - >>> result_tree2 = MirrorBinaryTree().mirror_binary_tree(tree2) + >>> result_tree2 = MirrorBinaryTree().invert_binary_tree(tree2) >>> print_preorder(result_tree2) 9 """ @@ -51,10 +51,10 @@ def mirror_binary_tree(self, root : TreeNode): return None if root.left: - self.mirror_binary_tree(root.left) + self.invert_binary_tree(root.left) if root.right: - self.mirror_binary_tree(root.right) + self.invert_binary_tree(root.right) root.left, root.right = root.right, root.left