-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path617.cpp
More file actions
31 lines (31 loc) · 899 Bytes
/
Copy path617.cpp
File metadata and controls
31 lines (31 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2) {
if (!t1)
return t2;
else if (!t2)
return t1;
queue<TreeNode *> q1, q2;
q1.push(t1);
q2.push(t2);
while (!q1.empty()) {
TreeNode *temp1 = q1.front(), *temp2 = q2.front();
q1.pop();
q2.pop();
if (temp1) {
q1.push(temp1->left);
q1.push(temp1->right);
q2.push(temp2 ? temp2->left : nullptr);
q2.push(temp2 ? temp2->right : nullptr);
}
if (temp1 && temp2) {
temp1->val += temp2->val;
if (!temp1->left)
temp1->left = temp2->left;
if (!temp1->right)
temp1->right = temp2->right;
}
}
return t1;
}
};