Symmetric Tree @ LeetCode (Python)
kitt
posted @ 2014年2月22日 21:34
in LeetCode
, 2400 阅读
递归方法, 判断左右子树是否对称: 1)左右子树的根节点都为空或者val相同, 2)左子树的左子树与右子树的右子树对称, 3)左子树的右子树与右子树的左子树对称。
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a boolean def isSymmetric(self, root): if root == None: return True return self.checkSym(root.left, root.right) def checkSym(self, left, right): if left == right == None: return True if not (left and right): return False if left.val != right.val: return False return self.checkSym(left.left, right.right) and self.checkSym(left.right, right.left)