- Merge Two Binary Trees
def mergeTrees(self, t1, t2):
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
这里主要是t1 and t1.left,and 原来是这个意思,意思是
x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。
解释地址