| 22 | class Solution { |
| 23 | public: |
| 24 | TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { |
| 25 | if(root1 == nullptr && root2 == nullptr) { |
| 26 | return nullptr; |
| 27 | } else if (root1 == nullptr) { |
| 28 | return root2; |
| 29 | } else if (root2 == nullptr) { |
| 30 | return root1; |
| 31 | } else { |
| 32 | TreeNode* root = new TreeNode(root1->val + root2->val); |
| 33 | root->left = mergeTrees(root1->left, root2->left); |
| 34 | root->right = mergeTrees(root1->right, root2->right); |
| 35 | return root; |
| 36 | } |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 |