LeetCode617. 合并二叉树
- 题目链接
- 代码
题目链接
https://leetcode.cn/problems/merge-two-binary-trees/
代码
递归
# 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 root2if not root2:return root1root1.val += root2.valroot1.left = self.mergeTrees(root1.left, root2.left)root1.right = self.mergeTrees(root1.right, root2.right)return root1
迭代
# 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 root2if not root2:return root1queue = deque()queue.append(root1)queue.append(root2)while queue:node1 = queue.popleft()node2 = queue.popleft()if node1.left and node2.left:queue.append(node1.left)queue.append(node2.left)if node1.right and node2.right:queue.append(node1.right)queue.append(node2.right)node1.val += node2.valif not node1.left and node2.left:node1.left = node2.leftif not node1.right and node2.right:node1.right = node2.rightreturn root1