CREATED TIME: Jan 15, 2020 1:11 AM UPDATED TIME: Jan 15, 2020 1:11 AM 난이도: Easy
/** * 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) { TreeNode* head = NULL; if (t1 == NULL and t2 == NULL){ return head; }else{ head = new TreeNode(0); }
TreeNode* t1_left = NULL;
TreeNode* t1_right = NULL;
TreeNode* t2_left = NULL;
TreeNode* t2_right = NULL;
if(t1 != NULL){
head->val += t1->val;
t1_left = t1->left;
t1_right = t1->right;
}
if(t2 != NULL){
head->val += t2->val;
t2_left = t2->left;
t2_right = t2->right;
}
TreeNode* left = mergeTrees(t1_left, t2_left);
head->left = left;
TreeNode* right = mergeTrees(t1_right, t2_right);
head->right = right;
return head;
}
};