diff --git a/c/617-Merge-Two-Binary-Trees.c b/c/617-Merge-Two-Binary-Trees.c new file mode 100644 index 000000000..fc3bedb3a --- /dev/null +++ b/c/617-Merge-Two-Binary-Trees.c @@ -0,0 +1,18 @@ +/* +You are given two binary trees root1 and root2. +Return the merged tree + +Time: O(n) +Space: O(1) As we reuse the trees +*/ + +struct TreeNode* mergeTrees(struct TreeNode* root1, struct TreeNode* root2){ + if (root1==NULL) + return root2; + else if (root2==NULL) + return root1; + root1->val += root2->val; + root1->left = mergeTrees(root1->left, root2->left); + root1->right = mergeTrees(root1->right, root2->right); + return root1; +}