Article Directory
One [topic category]
- depth-first search
Two [question difficulty]
- Simple
Three [topic number]
- 617. Merge Binary Trees
Four [title description]
- You are given two binary trees: root1 and root2.
- Imagine that when you overlay one tree on top of the other, some nodes on both trees will overlap (and others won't). You need to merge these two trees into a new binary tree. The rule of merging is: if two nodes overlap, then add the values of these two nodes as the new value of the merged node; otherwise, the node that is not null will be directly used as the node of the new binary tree.
- Returns the merged binary tree.
- Note: The merging process must start from the root node of both trees.
Five [topic examples]
-
Example 1:
- Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
- Output: [3,4,5,5,4,null,7]
-
Example 2:
- Input: root1 = [1], root2 = [1,2]
- Output: [2,2]
Six [problem-solving ideas]
- Use the idea of depth-first search to solve this problem. After analyzing the problem, we can find that:
- When one of the leaf nodes of the two trees is empty, the leaf node that is not empty should be retained at this time, that is, the leaf node that is not empty should be returned, which is the end condition of the recursion
- When the leaf nodes of both trees are empty, return empty, which is also the end condition of recursion
- When the nodes of the two trees are not empty, we use the node of the first tree as the return node, add the corresponding node values of the two trees, and assign it to the corresponding node of the first tree
- According to this rule, you can recurse to the left and right subtrees respectively, and "merge" the nodes at the corresponding positions of the two trees.
- Because the first tree is used as the merged tree, the first tree can be returned at the end
Seven [title prompt]
- The number of nodes in the two trees is in the range [0, 2000] The number of nodes in the two trees is in the range [0, 2000]The number of nodes in the two trees is in the range [ 0 ,2000 ] _
- − 1 0 4 < = N o d e . v a l < = 1 0 4 -10^4 <= Node.val <= 10^4 −104<=Node.val<=104
Eight 【Time Frequency】
- Time complexity: O ( min ( m , n ) ) O(min(m,n))O(min(m,n )) wherem , nm,nm,n is the number of nodes of the two binary trees passed in respectively
- Space complexity: O ( min ( m , n ) ) O(min(m,n))O(min(m,n )) wherem , nm,nm,n is the number of nodes of the two binary trees passed in respectively
Nine [code implementation]
- Java language version
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null){
return root2;
}
if(root2 == null){
return root1;
}
root1.val = root1.val + root2.val;
root1.left = mergeTrees(root1.left,root2.left);
root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}
- C language version
struct TreeNode* mergeTrees(struct TreeNode* root1, struct TreeNode* root2)
{
if(root1 == NULL)
{
return root2;
}
if(root2 == NULL)
{
return root1;
}
root1->val = root1->val + root2->val;
root1->left = mergeTrees(root1->left,root2->left);
root1->right = mergeTrees(root1->right,root2->right);
return root1;
}
- Python language version
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if root1 is None:
return root2
if root2 is None:
return root1
root1.val = root1.val + root2.val
root1.left = self.mergeTrees(root1.left,root2.left)
root1.right = self.mergeTrees(root1.right,root2.right)
return root1
- C++ language version
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr)
{
return root2;
}
if(root2 == nullptr)
{
return root1;
}
root1->val = root1->val + root2->val;
root1->left = mergeTrees(root1->left,root2->left);
root1->right = mergeTrees(root1->right,root2->right);
return root1;
}
};
Ten【Submission results】
-
Java language version
-
C language version
-
Python language version
-
C++ language version