data_structures.binary_tree.symmetric_tree

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Leetcode reference: https://leetcode.com/problems/symmetric-tree/

Classes

Node

A Node represents an element of a binary tree, which contains:

Functions

is_mirror(→ bool)

Check if two subtrees are mirror images of each other.

is_symmetric_tree(→ bool)

Check if a binary tree is symmetric (i.e., a mirror of itself).

make_asymmetric_tree(→ Node)

Create an asymmetric tree for testing.

make_symmetric_tree(→ Node)

Create a symmetric tree for testing.

Module Contents

class data_structures.binary_tree.symmetric_tree.Node

A Node represents an element of a binary tree, which contains:

Attributes: data: The value stored in the node (int). left: Pointer to the left child node (Node or None). right: Pointer to the right child node (Node or None).

Example: >>> node = Node(1, Node(2), Node(3)) >>> node.data 1 >>> node.left.data 2 >>> node.right.data 3

data: int
left: Node | None = None
right: Node | None = None
data_structures.binary_tree.symmetric_tree.is_mirror(left: Node | None, right: Node | None) bool

Check if two subtrees are mirror images of each other.

Parameters: left: The root node of the left subtree. right: The root node of the right subtree.

Returns: bool: True if the two subtrees are mirrors of each other, False otherwise.

Example: >>> tree1 = make_symmetric_tree() >>> is_mirror(tree1.left, tree1.right) True >>> tree2 = make_asymmetric_tree() >>> is_mirror(tree2.left, tree2.right) False

data_structures.binary_tree.symmetric_tree.is_symmetric_tree(tree: Node) bool

Check if a binary tree is symmetric (i.e., a mirror of itself).

Parameters: tree: The root node of the binary tree.

Returns: bool: True if the tree is symmetric, False otherwise.

Example: >>> is_symmetric_tree(make_symmetric_tree()) True >>> is_symmetric_tree(make_asymmetric_tree()) False

data_structures.binary_tree.symmetric_tree.make_asymmetric_tree() Node

Create an asymmetric tree for testing.

The tree looks like this:

1

/

2 2

/ /

3 4 3 4

Returns: Node: Root node of an asymmetric tree.

Example: >>> tree = make_asymmetric_tree() >>> tree.data 1 >>> tree.left.data == tree.right.data True >>> tree.left.left.data == tree.right.right.data False

data_structures.binary_tree.symmetric_tree.make_symmetric_tree() Node

Create a symmetric tree for testing.

The tree looks like this:

1

/

2 2

/ /

3 4 4 3

Returns: Node: Root node of a symmetric tree.

Example: >>> tree = make_symmetric_tree() >>> tree.data 1 >>> tree.left.data == tree.right.data True >>> tree.left.left.data == tree.right.right.data True