"""
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3
"""
from implementations.tree import BinaryTreeNode


def symmetric(tree):
    return _symmetric(tree.left, tree.right)


def _symmetric(tree1, tree2):
    if tree1 is None and tree2 is None:
        return True
    elif tree1 is None or tree2 is None:
        return False
    return tree1.value == tree2.value and _symmetric(tree1.left, tree2.right) and _symmetric(tree1.right, tree2.left)


if __name__ == '__main__':
    tree = BinaryTreeNode(1)
    tree.left = BinaryTreeNode(4)
    tree.left.left = BinaryTreeNode(3)
    tree.left.right = BinaryTreeNode(2)
    tree.right = BinaryTreeNode(4)
    tree.right.left = BinaryTreeNode(2)
    tree.right.right = BinaryTreeNode(3)
    print symmetric(tree)
