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¶
A Node has value variable and pointers to Nodes to its left and right. |
Functions¶
|
Mirror binary trees with the given root and returns the root |
|
Return a binary tree with 9 nodes that looks like this: |
|
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)
- 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