/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param t1 TreeNode类 * @param t2 TreeNode类 * @return TreeNode类 */ TreeNode* answer(TreeNode*t1,TreeNode*t2) { if(t1==NULL && t2==NULL) { return NULL; } else { TreeNode*root; if(t1==NULL) { root=t2; return root; } else { if(t2==NULL) { root=t1; return t1; } else { t1->val+=t2->val; root=t1; root->left=answer(t1->left,t2->left); root->right=answer(t1->right,t2->right); return root; } } } } TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { return answer(t1,t2); } };