- 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
- 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