C solution


  • 0
    G

    /**

    • Definition for a binary tree node.

    • struct TreeNode {

    • int val;
      
    • struct TreeNode *left;
      
    • struct TreeNode *right;
      
    • };
      /
      struct TreeNode
      mergeTrees(struct TreeNode* t1, struct TreeNode* t2)
      {
      if(t1==NULL && t2==NULL)
      return NULL;
      struct TreeNode* result = (struct TreeNode*)malloc(sizeof(struct TreeNode));

      result->val = (t1==NULL ? 0 : t1->val) + (t2==NULL ? 0 : t2->val);
      result->left = mergeTrees(t1==NULL?NULL:t1->left, t2==NULL?NULL:t2->left);
      result->right = mergeTrees(t1==NULL?NULL:t1->right, t2==NULL?NULL:t2->right);

      return result;
      }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.