二叉树试题

设计一个函数,判断一棵二叉树是不是对称的(如果一棵二叉树和它的镜像一样,那么它是对称的)

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

For example, this binary tree is symmetric:

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

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

Python:

# 是否对称
def isSymmetric(self):
    if self.root is None:
        print("True")
    else:
        return self.compRoot(self.root.left, self.root.right)

    #根节点比较
def compRoot(self, left, right):
    if left is None:
        if right is None:
            return True
        else:
            return False
    elif right is None:
        return False
    else:
        return self.compRoot(left.left, right.right) & self.comRoot(left.right, right.left)