101. Symmetric Tree
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:
Note:
Bonus points if you could solve it both recursively and iteratively.
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isSymmetric(self, root): if not root: return True return self.is_reverse(root.left, root.right) def is_reverse(self, a, b): # return true if `a` is a reversed tree of `b` if not a or not b: return not a and not b return a.val == b.val and self.is_reverse(a.left,b.right) and self.is_reverse(a.right,b.left)
以上