mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
21 lines
675 B
C
21 lines
675 B
C
struct TreeNode *newNode(int item)
|
|
{
|
|
struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
|
|
node->val = item;
|
|
node->left = node->right = NULL;
|
|
return node;
|
|
}
|
|
|
|
struct TreeNode *mergeTrees(struct TreeNode *t1, struct TreeNode *t2)
|
|
{
|
|
if (t1 == NULL && t2 == NULL)
|
|
return NULL;
|
|
int item = (t1 == NULL ? 0 : t1->val) + (t2 == NULL ? 0 : t2->val);
|
|
struct TreeNode *node = newNode(item);
|
|
node->left =
|
|
mergeTrees(t1 == NULL ? NULL : t1->left, t2 == NULL ? NULL : t2->left);
|
|
node->right = mergeTrees(t1 == NULL ? NULL : t1->right,
|
|
t2 == NULL ? NULL : t2->right);
|
|
return node;
|
|
}
|