其他分享
首页 > 其他分享> > LeetCode617 合并二叉树

LeetCode617 合并二叉树

作者:互联网

LeetCode617 合并二叉树

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:

        if not root1: return root2
        if not root2: return root1

        merged = TreeNode(root1.val + root2.val)
        merged.left = self.mergeTrees(root1.left, root2.left)
        merged.right = self.mergeTrees(root1.right, root2.right)

        return merged

标签:LeetCode617,right,val,self,合并,二叉树,left,root1,root2
来源: https://www.cnblogs.com/solvit/p/16651641.html