[617]Merge Two Binary Trees

Given two binary trees and 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 them 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 new tree.

Example 1:

Input: 
	Tree 1                     Tree 2                  
          1                         2                             
         / \                       / \                            
        3   2                     1   3                        
       /                           \   \                      
      5                             4   7                  
Output: 
Merged tree:
	     3
	    / \
	   4   5
	  / \   \ 
	 5   4   7

 

Note: The merging process must start from the root nodes of both trees.

Ideas: traverse the level, the queue to go into the same position each time two trees on the node is not empty, because the final output is the left tree, then the node a total of the following three conditions:

1. The corresponding position on the left and right tree only child left the tree is empty, put the right tree child is assigned to the corresponding position of the left tree

2. trees around the corresponding location on children are not empty directly into the queue it, add up to a queue when the left tree

Only about 3 position on child right tree is empty, the tree does not need to deal with correspondence, we need to output a tree on the left (X)

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution 
11 {
12 public:
13     TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) 
14     {
15         if(t1 == nullptr) return t2;
16         if(t2 == nullptr) return t1;
17         if(t1 == nullptr && t2 == nullptr) return nullptr;
18         else
19         {
20           queue<TreeNode*>q;
21           q.push(t1);
22           q.push(t2);
23           while(!q.empty())
24           {
25             TreeNode* l1 = q.front();
26             q.pop();
27             TreeNode* l2 = q.front();
28             q.pop();
29             l1->val +=l2->val;
30             if(l1->left != nullptr && l2->left!=nullptr)
31             {
32               q.push(l1->left);
33               q.push(l2->left);
34             }
35             if(l1->right != nullptr && l2->right!=nullptr)
36             {
37               q.push(l1->right);
38               q.push(l2->right);
39             }
40             if(l1->left == nullptr && l2->left != nullptr )
41             {
42               l1->left = l2->left;
43             }
44             if(l1->right == nullptr && l2->right != nullptr)
45             {
46               l1->right = l2->right;
47             }
48           }
49           return t1;
50         }
51     }
52 };

Guess you like

Origin www.cnblogs.com/Swetchine/p/11247895.html