您的位置:首页 > 编程语言 > Python开发

【LeetCode with Python】 Symmetric Tree

2014-07-06 15:34 309 查看
博客域名:http://www.xnerv.wang

原题页面:https://oj.leetcode.com/problems/symmetric-tree/

题目类型:

难度评价:★

本文地址:/article/1377497.html

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: