data_structures.binary_tree.mirror_binary_tree ============================================== .. py:module:: data_structures.binary_tree.mirror_binary_tree .. autoapi-nested-parse:: 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 ------- .. autoapisummary:: data_structures.binary_tree.mirror_binary_tree.Node Functions --------- .. autoapisummary:: data_structures.binary_tree.mirror_binary_tree.main data_structures.binary_tree.mirror_binary_tree.make_tree_nine data_structures.binary_tree.mirror_binary_tree.make_tree_seven Module Contents --------------- .. py:class:: Node A Node has value variable and pointers to Nodes to its left and right. .. py:method:: __iter__() -> collections.abc.Iterator[int] .. py:method:: __len__() -> int .. py:method:: 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) .. py:attribute:: left :type: Node | None :value: None .. py:attribute:: right :type: Node | None :value: None .. py:attribute:: value :type: int .. py:function:: 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 .. py:function:: 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] .. py:function:: 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]