Merge Two Binary Trees LeetCode solution in C++

1
/**
2
* Definition for a binary tree node.
3
* struct TreeNode {
4
* int val;
5
* TreeNode *left;
6
* TreeNode *right;
7
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10
* right(right) {}
11
* };
12
*/
13
class Solution {
14
public:
15
TreeNode *mergeTrees(TreeNode *root1, TreeNode *root2) {
16
if (root1 == NULL) return root2;
17
if (root2 == NULL) return root1;
18
root1->val += root2->val;
19
root1->left = mergeTrees(root1->left, root2->left);
20
root1->right = mergeTrees(root1->right, root2->right);
21
return root1;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0