-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Mirror a Binary Tree
solution
#9534
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
Merged
cclauss
merged 12 commits into
TheAlgorithms:master
from
gogouravr:feat/data_structures/binary_tree/invert-binary-tree
Oct 24, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ddefa04
Add `Invert a Binary Tree` solution
gogouravr a6fdd44
Add type
gogouravr 7b9dc8a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8585e67
Add `doctest`
gogouravr 105badb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5ff2763
Add `test` to `get_tree_inorder`
gogouravr 758a7f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 94f0aed
Add `test` changes
gogouravr 4db2efd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bdd6cb5
Fix lint errors
gogouravr 8c8c319
Fix precommit errors
gogouravr 67e780d
Update and rename invert_binary_tree.py to mirror_binary_tree.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
""" | ||
Given the root of a binary tree, mirror the tree, and return its root. | ||
|
||
Leetcode problem reference: https://leetcode.com/problems/mirror-binary-tree/ | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
""" | ||
A Node has value variable and pointers to Nodes to its left and right. | ||
""" | ||
|
||
value: int | ||
left: Node | None = None | ||
right: Node | None = None | ||
|
||
def __iter__(self) -> Iterator[int]: | ||
if self.left: | ||
yield from self.left | ||
yield self.value | ||
if self.right: | ||
yield from self.right | ||
|
||
def __len__(self) -> int: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sum(1 for _ in self) | ||
|
||
def mirror(self) -> Node: | ||
""" | ||
Mirror the binary tree rooted at this node by swapping left and right children. | ||
|
||
>>> tree = Node(0) | ||
>>> list(tree) | ||
[0] | ||
>>> list(tree.mirror()) | ||
[0] | ||
>>> tree = Node(1, Node(0), Node(3, Node(2), Node(4, None, Node(5)))) | ||
>>> tuple(tree) | ||
(0, 1, 2, 3, 4, 5) | ||
>>> tuple(tree.mirror()) | ||
(5, 4, 3, 2, 1, 0) | ||
""" | ||
self.left, self.right = self.right, self.left | ||
if self.left: | ||
self.left.mirror() | ||
if self.right: | ||
self.right.mirror() | ||
return self | ||
|
||
|
||
def make_tree_seven() -> Node: | ||
r""" | ||
Return a binary tree with 7 nodes that looks like this: | ||
1 | ||
/ \ | ||
2 3 | ||
/ \ / \ | ||
4 5 6 7 | ||
|
||
>>> tree_seven = make_tree_seven() | ||
>>> len(tree_seven) | ||
7 | ||
>>> list(tree_seven) | ||
[4, 2, 5, 1, 6, 3, 7] | ||
""" | ||
tree = Node(1) | ||
tree.left = Node(2) | ||
tree.right = Node(3) | ||
tree.left.left = Node(4) | ||
tree.left.right = Node(5) | ||
tree.right.left = Node(6) | ||
tree.right.right = Node(7) | ||
return tree | ||
|
||
|
||
def make_tree_nine() -> Node: | ||
r""" | ||
Return a binary tree with 9 nodes that looks like this: | ||
1 | ||
/ \ | ||
2 3 | ||
/ \ \ | ||
4 5 6 | ||
/ \ \ | ||
7 8 9 | ||
|
||
>>> tree_nine = make_tree_nine() | ||
>>> len(tree_nine) | ||
9 | ||
>>> list(tree_nine) | ||
[7, 4, 8, 2, 5, 9, 1, 3, 6] | ||
""" | ||
tree = Node(1) | ||
tree.left = Node(2) | ||
tree.right = Node(3) | ||
tree.left.left = Node(4) | ||
tree.left.right = Node(5) | ||
tree.right.right = Node(6) | ||
tree.left.left.left = Node(7) | ||
tree.left.left.right = Node(8) | ||
tree.left.right.right = Node(9) | ||
return tree | ||
|
||
|
||
def main() -> None: | ||
r""" | ||
Mirror binary trees with the given root and returns the root | ||
|
||
>>> tree = make_tree_nine() | ||
>>> tuple(tree) | ||
(7, 4, 8, 2, 5, 9, 1, 3, 6) | ||
>>> tuple(tree.mirror()) | ||
(6, 3, 1, 9, 5, 2, 8, 4, 7) | ||
|
||
nine_tree: | ||
1 | ||
/ \ | ||
2 3 | ||
/ \ \ | ||
4 5 6 | ||
/ \ \ | ||
7 8 9 | ||
|
||
The mirrored tree looks like this: | ||
1 | ||
/ \ | ||
3 2 | ||
/ / \ | ||
6 5 4 | ||
/ / \ | ||
9 8 7 | ||
""" | ||
trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()} | ||
for name, tree in trees.items(): | ||
print(f" The {name} tree: {tuple(tree)}") | ||
# (0,) | ||
# (4, 2, 5, 1, 6, 3, 7) | ||
# (7, 4, 8, 2, 5, 9, 1, 3, 6) | ||
print(f"Mirror of {name} tree: {tuple(tree.mirror())}") | ||
# (0,) | ||
# (7, 3, 6, 1, 5, 2, 4) | ||
# (6, 3, 1, 9, 5, 2, 8, 4, 7) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.