已知两颗二叉树,将它们合并成一颗二叉树。合并规则是:都存在的结点,就将结点值加起来,否则空的位置就由另一个树的结点来代替。例如:
两颗二叉树是:
Tree 1
两颗二叉树是:
Tree 1
Tree 2
合并后的树为
数据范围:树上节点数量满足 ,树上节点的值一定在32位整型范围内。
进阶:空间复杂度 ,时间复杂度
{1,3,2,5},{2,1,3,#,4,#,7}
{3,4,5,5,4,#,7}
如题面图
{1},{}
{1}
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param t1 TreeNode类 # @param t2 TreeNode类 # @return TreeNode类 # class Solution: def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode: # write code here if not t1: return t2 if not t2: return t1 head = TreeNode(t1.val + t2.val) head.left, head.right = self.mergeTrees(t1.left, t2.left), self.mergeTrees(t1.right, t2.right) return head
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param t1 TreeNode类 # @param t2 TreeNode类 # @return TreeNode类 # class Solution: def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode: # write code here if not t1: return t2 if not t2: return t1 merged = TreeNode(t1.val + t2.val) merged.left = self.mergeTrees(t1.left, t2.left) merged.right = self.mergeTrees(t1.right, t2.right) return merged
class Solution: def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode: # write code here if t1 is None and t2 is None: return None 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
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param t1 TreeNode类 # @param t2 TreeNode类 # @return TreeNode类 # class Solution: def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode: if t1 is None&nbs***bsp;t2 is None: if t1: return t1 else: return t2 left = self.mergeTrees(t1.left, t2.left) right = self.mergeTrees(t1.right, t2.right) root = TreeNode(t1.val + t2.val) root.left = left root.right = right return root