Advertisement

Merge Two Binary Trees - LeetCode 617 Solution

Merge Two Binary Trees - Complete Solution Guide

Merge Two Binary Trees is LeetCode problem 617, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two binary trees root1 and root2 . Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. Return the merged tree . Note: The merging process must start from the root nodes of both t

Detailed Explanation

The problem asks us to merge two binary trees, `root1` and `root2`, into a single new binary tree. If two nodes in the same position in both trees exist (overlap), their values should be summed to create the value of the corresponding node in the merged tree. If a node only exists in one of the trees, that node becomes part of the merged tree. The merging process starts from the root nodes of both trees.

Solution Approach

The solution uses a recursive approach. It first checks if either `root1` or `root2` is null. If `root1` is null, `root2` is returned (or null if `root2` is also null). If `root2` is null, `root1` is returned. If both are not null, the value of `root2` is added to `root1`. Then, the left and right subtrees are merged recursively and assigned to the left and right children of `root1` respectively. Finally, `root1` is returned, which is now the merged tree.

Step-by-Step Algorithm

  1. Step 1: Check if either `root1` or `root2` is null. If `root1` is null, return `root2`. If `root2` is null, return `root1`.
  2. Step 2: If both `root1` and `root2` are not null, add the value of `root2`'s node to `root1`'s node.
  3. Step 3: Recursively call `mergeTrees` with the left subtrees of `root1` and `root2` and assign the result to the left child of `root1`.
  4. Step 4: Recursively call `mergeTrees` with the right subtrees of `root1` and `root2` and assign the result to the right child of `root1`.
  5. Step 5: Return `root1`. `root1` now represents the merged tree.

Key Insights

  • Insight 1: Recursion is a natural fit because the merging process is applied to each node and its subtrees.
  • Insight 2: We can modify the `root1` tree directly to create the merged tree, saving space. If either root is null, the other root (or null if both are null) should be returned directly.
  • Insight 3: The base cases for the recursion are when either `root1` or `root2` is null. This handles cases where one tree is larger than the other or when we reach the end of a branch.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: MongoDB.