/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* mergeTrees(struct TreeNode* root1, struct TreeNode* root2) {
if(!root1&&!root2) return NULL;
struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val=0;
if(root1) root->val=root1->val;
if(root2) root->val+=root2->val;
if(root1&&root2){
root->left=mergeTrees(root1->left,root2->left);
root->right=mergeTrees(root1->right,root2->right);
}else if(root1&&!root2){
root->left=mergeTrees(root1->left,NULL);
root->right=mergeTrees(root1->right,NULL);
}else{
root->left=mergeTrees(NULL,root2->left);
root->right=mergeTrees(NULL,root2->right);
}
return root;
}
结果:
标签:right,TreeNode,struct,root,合并,617,二叉树,root1,root2 From: https://www.cnblogs.com/llllmz/p/18056329