617. Merge Two Binary Trees
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 79% Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(h)
class Solution(object):
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
if t1 is None:
return t2
if t2 is None:
return t1
t1.val += t2.val
t1.left = self.mergeTrees(t1.left, t2.left)
t1.right = self.mergeTrees(t1.right, t2.right)
return t1
Solution from kamyu104/LeetCode-Solutions · MIT