- 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
思路:將兩棵樹上的結點值相加,用遞歸的方法。如果子節(jié)點空,循環(huán)就結束了。
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