Leetcode

101. Symmetric Tree

2016-08-05  本文已影响34人  oo上海

101. Symmetric Tree

题目:

https://leetcode.com/problems/symmetric-tree/

难度:

Easy

两棵树symmetric, 有几种可能:

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None :
            return True
        return self.symmetric(root.left, root.right)
        
    
    def symmetric(self,t1, t2):
        # empty tree
        if t1 == None or t2 == None:
            if t1 == t2 == None: 
                return True
            else:
                return False
        elif t1.val == t2.val:
            return self.symmetric(t1.left,t2.right) and self.symmetric(t1.right, t2.left)
        else:
            return False

其实函数不应该定义为symmetric,应该定义为qual?更合适。

上一篇下一篇

猜你喜欢

热点阅读