|
| 1 | +""" |
| 2 | +Given the root of a binary tree, check whether it is a mirror of itself |
| 3 | +(i.e., symmetric around its center). |
| 4 | +
|
| 5 | +Leetcode reference: https://leetcode.com/problems/symmetric-tree/ |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from dataclasses import dataclass |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class Node: |
| 14 | + """ |
| 15 | + A Node has data variable and pointers to Nodes to its left and right. |
| 16 | + """ |
| 17 | + |
| 18 | + data: int |
| 19 | + left: Node | None = None |
| 20 | + right: Node | None = None |
| 21 | + |
| 22 | + |
| 23 | +def make_symmetric_tree() -> Node: |
| 24 | + r""" |
| 25 | + Create a symmetric tree for testing. |
| 26 | + The tree looks like this: |
| 27 | + 1 |
| 28 | + / \ |
| 29 | + 2 2 |
| 30 | + / \ / \ |
| 31 | + 3 4 4 3 |
| 32 | + """ |
| 33 | + root = Node(1) |
| 34 | + root.left = Node(2) |
| 35 | + root.right = Node(2) |
| 36 | + root.left.left = Node(3) |
| 37 | + root.left.right = Node(4) |
| 38 | + root.right.left = Node(4) |
| 39 | + root.right.right = Node(3) |
| 40 | + return root |
| 41 | + |
| 42 | + |
| 43 | +def make_asymmetric_tree() -> Node: |
| 44 | + r""" |
| 45 | + Create a asymmetric tree for testing. |
| 46 | + The tree looks like this: |
| 47 | + 1 |
| 48 | + / \ |
| 49 | + 2 2 |
| 50 | + / \ / \ |
| 51 | + 3 4 3 4 |
| 52 | + """ |
| 53 | + root = Node(1) |
| 54 | + root.left = Node(2) |
| 55 | + root.right = Node(2) |
| 56 | + root.left.left = Node(3) |
| 57 | + root.left.right = Node(4) |
| 58 | + root.right.left = Node(3) |
| 59 | + root.right.right = Node(4) |
| 60 | + return root |
| 61 | + |
| 62 | + |
| 63 | +def is_symmetric_tree(tree: Node) -> bool: |
| 64 | + """ |
| 65 | + Test cases for is_symmetric_tree function |
| 66 | + >>> is_symmetric_tree(make_symmetric_tree()) |
| 67 | + True |
| 68 | + >>> is_symmetric_tree(make_asymmetric_tree()) |
| 69 | + False |
| 70 | + """ |
| 71 | + if tree: |
| 72 | + return is_mirror(tree.left, tree.right) |
| 73 | + return True # An empty tree is considered symmetric. |
| 74 | + |
| 75 | + |
| 76 | +def is_mirror(left: Node | None, right: Node | None) -> bool: |
| 77 | + """ |
| 78 | + >>> tree1 = make_symmetric_tree() |
| 79 | + >>> tree1.right.right = Node(3) |
| 80 | + >>> is_mirror(tree1.left, tree1.right) |
| 81 | + True |
| 82 | + >>> tree2 = make_asymmetric_tree() |
| 83 | + >>> is_mirror(tree2.left, tree2.right) |
| 84 | + False |
| 85 | + """ |
| 86 | + if left is None and right is None: |
| 87 | + # Both sides are empty, which is symmetric. |
| 88 | + return True |
| 89 | + if left is None or right is None: |
| 90 | + # One side is empty while the other is not, which is not symmetric. |
| 91 | + return False |
| 92 | + if left.data == right.data: |
| 93 | + # The values match, so check the subtree |
| 94 | + return is_mirror(left.left, right.right) and is_mirror(left.right, right.left) |
| 95 | + return False |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + from doctest import testmod |
| 100 | + |
| 101 | + testmod() |
0 commit comments