Day24

作者: wendy_要努力努力再努力 | 来源:发表于2017-11-30 09:35 被阅读0次
  1. Judge Route Circle
    思路:经历上下左右移动,判断是否能回到原点。即判断左移的步数是否等于右移的步数;上移的步数是否等于下移的步数
class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        if moves.count('U') == moves.count('D') and moves.count('L') == moves.count('R'):
            return True
        return False
  1. Merge Two Binary Trees
    思路:将两棵树上的结点值相加,用递归的方法。如果子节点空,循环就结束了。
class Solution(object):
    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if not t1 and not t2: return None
        ans = TreeNode((t1.val if t1 else 0) + (t2.val if t2 else 0))
        ans.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)
        ans.right = self.mergeTrees(t1 and t1.right, t2 and t2.right)
        return ans

相关文章

网友评论

      本文标题:Day24

      本文链接:https://www.haomeiwen.com/subject/ohimbxtx.html