0617.-合并二叉树

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

示例 1:

注意: 合并必须从两个树的根节点开始。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-binary-trees
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

参考:

python

# 0617.合并二叉树

# 递归-DFS
# 递归-前序
class Solution1:
    def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
        # 递归终止条件
        if not root1:
            return root2
        if not root2:
            return root1
        # 修改root1结构与值-N
        root1.val += root2.val
        # L
        root1.left = self.mergeTrees(root1.left, root2.left)
        # R
        root1.right = self.mergeTrees(root1.right, root2.right)
        return root1

# 递归-中序
class Solution2:
    def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
        # 递归终止条件
        if not root1:
            return root2
        if not root2:
            return root1

        # L
        root1.left = self.mergeTrees(root1.left, root2.left)
        # 修改root1结构与值-N
        root1.val += root2.val
        # R
        root1.right = self.mergeTrees(root1.right, root2.right)
        return root1

# 递归-后序
class Solution3:
    def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
        # 递归终止条件
        if not root1:
            return root2
        if not root2:
            return root1

        # L
        root1.left = self.mergeTrees(root1.left, root2.left)
        # R
        root1.right = self.mergeTrees(root1.right, root2.right)
        # 修改root1结构与值-N
        root1.val += root2.val
        return root1

# 迭代-BFS
# 迭代-层序遍历
class Solution4:
    def mergerTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
        if not root1:
            return root2
        if not root2:
            return root1

        from collections import deque
        queue = deque()
        queue.append(root1)
        queue.append(root2)

        while queue:
            node1 = queue.popleft()
            node2 = queue.popleft()
            # 合并节点值
            node1.val += node2.val
            # 左节点非空加入队列
            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)
            # root1左节点空,root2左节点非空
            if not node1.left and node2.left:
                node1.left = node2.left
            # root1右节点空,root2右节点非空
            if not node1.right and node2.right:
                node1.right = node2.right
        return root1

golang

package binaryTree

// 递归-DFS
// 递归-前序
func mergeTrees(root1, root2 *TreeNode) *TreeNode {
	if root1 == nil {
		return root2
	}
	if root2 == nil {
		return root1
	}

	root1.Val += root2.Val
	root1.Left = mergeTrees(root1.Left, root2.Left)
	root1.Right = mergeTrees(root1.Right, root2.Right)

	return root1
}

// 递归-中序
func mergeTrees1(root1, root2 *TreeNode) *TreeNode {
	if root1 == nil {
		return root2
	}
	if root2 == nil {
		return root1
	}

	root1.Left = mergeTrees1(root1.Left, root2.Left)
	root1.Val += root2.Val
	root1.Right = mergeTrees1(root1.Right, root2.Right)

	return root1
}

// 递归-后序
func mergeTrees2(root1, root2 *TreeNode) *TreeNode {
	if root1 == nil {
		return root2
	}
	if root2 == nil {
		return root1
	}

	root1.Left = mergeTrees1(root1.Left, root2.Left)
	root1.Right = mergeTrees1(root1.Right, root2.Right)
	root1.Val += root2.Val

	return root1
}

// 迭代-BFS
func mergeTrees3(root1, root2 *TreeNode) *TreeNode {
	if root1 == nil {
		return root2
	}
	if root2 == nil {
		return root1
	}

	queue := make([]*TreeNode, 0)
	queue = append(queue, root1)
	queue = append(queue, root2)

	for size:=len(queue);size>0;size=len(queue) {
		node1 := queue[0]
		queue = queue[1:]
		node2 := queue[0]
		queue = queue[1:]
		node1.Val += node2.Val

		if node1.Left != nil && node2.Left != nil {
			queue = append(queue, node1.Left)
			queue = append(queue, node2.Left)
		}
		if node1.Right != nil && node2.Right != nil {
			queue = append(queue, node1.Right)
			queue = append(queue, node2.Right)
		}
		if node1.Left == nil && node2.Left != nil {
			node1.Left = node2.Left
		}
		if node1.Right == nil && node2.Right != nil {
			node1.Right = node2.Right
		}
	}
	return root1
}

posted on 2021-11-17 08:42  进击的davis  阅读(70)  评论(0编辑  收藏  举报

导航