-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path617.cpp
More file actions
34 lines (31 loc) · 690 Bytes
/
Copy path617.cpp
File metadata and controls
34 lines (31 loc) · 690 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
32
33
34
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (t1 == 0 && t2 == 0) return 0;
TreeNode *t1l, *t1r, *t2l, *t2r;
int val = 0;
if (t1) {
val += t1->val; t1l = t1->left; t1r = t1->right;
} else {
t1l = t1r = 0;
}
if (t2) {
val += t2->val; t2l = t2->left; t2r = t2->right;
} else {
t2l = t2r = 0;
}
TreeNode *node = new TreeNode(val);
node->left = mergeTrees(t1l, t2l);
node->right = mergeTrees(t1r, t2r);
return node;
}
};