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 comproblemssymmetrictree a node has data variable and pointers to nodes to its left and right root node1 root left node2 root right node2 root left left node3 root left right node4 root right left node4 root right right node3 return root def makeasymmetrictree node r create a asymmetric tree for testing the tree looks like this 1 2 2 3 4 3 4 test cases for issymmetrictree function issymmetrictreemakesymmetrictree true issymmetrictreemakeasymmetrictree false tree1 makesymmetrictree tree1 right right node3 ismirrortree1 left tree1 right true tree2 makeasymmetrictree ismirrortree2 left tree2 right false both sides are empty which is symmetric one side is empty while the other is not which is not symmetric the values match so check the subtree a node has data variable and pointers to nodes to its left and right create a symmetric tree for testing the tree looks like this 1 2 2 3 4 4 3 create a asymmetric tree for testing the tree looks like this 1 2 2 3 4 3 4 test cases for is_symmetric_tree function is_symmetric_tree make_symmetric_tree true is_symmetric_tree make_asymmetric_tree false an empty tree is considered symmetric tree1 make_symmetric_tree tree1 right right node 3 is_mirror tree1 left tree1 right true tree2 make_asymmetric_tree is_mirror tree2 left tree2 right false both sides are empty which is symmetric one side is empty while the other is not which is not symmetric the values match so check the subtree
from __future__ import annotations

from dataclasses import dataclass


@dataclass
class Node:
    

    data: int
    left: Node | None = None
    right: Node | None = None


def make_symmetric_tree() -> Node:
    r
    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(4)
    root.right.left = Node(4)
    root.right.right = Node(3)
    return root


def make_asymmetric_tree() -> Node:
    r
    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(4)
    root.right.left = Node(3)
    root.right.right = Node(4)
    return root


def is_symmetric_tree(tree: Node) -> bool:
    
    if tree:
        return is_mirror(tree.left, tree.right)
    return True  


def is_mirror(left: Node | None, right: Node | None) -> bool:
    
    if left is None and right is None:
        
        return True
    if left is None or right is None:
        
        return False
    if left.data == right.data:
        
        return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
    return False


if __name__ == "__main__":
    from doctest import testmod

    testmod()