博客域名:
http://www.xnerv.wang
原题页面: https://oj.leetcode.com/problems/symmetric-tree/
题目类型:
难度评价:★
本文地址: http://blog.csdn.net/nerv3x3/article/details/37339069
原题页面: https://oj.leetcode.com/problems/symmetric-tree/
题目类型:
难度评价:★
本文地址: http://blog.csdn.net/nerv3x3/article/details/37339069
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
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
class Solution:
def checkSysmmetric(self, left, right):
if None == left and None == right:
return True
elif None == left or None == right:
return False
elif left.val != right.val:
return False
return self.checkSysmmetric(left.left, right.right) and self.checkSysmmetric(left.right, right.left)
# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
if None == root:
return True
return self.checkSysmmetric(root.left, root.right)