data_structures.binary_tree.mirror_binary_tree

Given the root of a binary tree, mirror the tree, and return its root.

Leetcode problem reference: https://leetcode.com/problems/mirror-binary-tree/

Classes

Node

A Node has value variable and pointers to Nodes to its left and right.

Functions

main(→ None)

Mirror binary trees with the given root and returns the root

make_tree_nine(→ Node)

Return a binary tree with 9 nodes that looks like this:

make_tree_seven(→ Node)

Return a binary tree with 7 nodes that looks like this:

Module Contents

class data_structures.binary_tree.mirror_binary_tree.Node

A Node has value variable and pointers to Nodes to its left and right.

__iter__() collections.abc.Iterator[int]
__len__() int
mirror() 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)
left: Node | None = None
right: Node | None = None
value: int
data_structures.binary_tree.mirror_binary_tree.main() None

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

data_structures.binary_tree.mirror_binary_tree.make_tree_nine() Node
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]
data_structures.binary_tree.mirror_binary_tree.make_tree_seven() Node
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]