Skip to content

Create Algo: Invert Binary Tree - Contributes to Issue #9943 #12112

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

Closed
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
21 changes: 21 additions & 0 deletions data_structures/binary_tree/binary_tree_path_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ 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
0
/ \
5 5

>>> tree2 = Node(0)
>>> tree2.left = Node(5)
>>> tree2.right = Node(15)

>>> BinaryTreePathSum().path_sum(tree2, 5)
2
>>> BinaryTreePathSum().path_sum(tree2, -1)
0
>>> BinaryTreePathSum().path_sum(tree2, 0)
1

"""

target: int
Expand Down
88 changes: 88 additions & 0 deletions data_structures/binary_tree/invert_binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
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

Check failure on line 9 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:9:83: W291 Trailing whitespace
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

Check failure on line 14 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:14:35: W291 Trailing whitespace
from dataclasses import dataclass

Check failure on line 15 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:15:34: W291 Trailing whitespace

@dataclass

Check failure on line 17 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/binary_tree/invert_binary_tree.py:14:1: I001 Import block is un-sorted or un-formatted
class TreeNode:
"""
A TreeNode has a data variable and pointers to TreeNode objects for its left and right children.

Check failure on line 20 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/binary_tree/invert_binary_tree.py:20:89: E501 Line too long (100 > 88)
"""

Check failure on line 22 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/invert_binary_tree.py:22:1: W293 Blank line contains whitespace
def __init__(self, data: int) -> None:
self.data = data

Check failure on line 24 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:24:25: W291 Trailing whitespace
self.left: TreeNode | None = None

Check failure on line 25 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:25:42: W291 Trailing whitespace
self.right: TreeNode | None = None

Check failure on line 26 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/binary_tree/invert_binary_tree.py:26:43: W291 Trailing whitespace

Check failure on line 27 in data_structures/binary_tree/invert_binary_tree.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/binary_tree/invert_binary_tree.py:27:1: W293 Blank line contains whitespace

class MirrorBinaryTree:
def invert_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().invert_binary_tree(tree)
>>> print_preorder(result_tree)
0
20
10
>>> tree2 = TreeNode(9)
>>> result_tree2 = MirrorBinaryTree().invert_binary_tree(tree2)
>>> print_preorder(result_tree2)
9
"""

if not root:
return None

if root.left:
self.invert_binary_tree(root.left)

if root.right:
self.invert_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()