其他分享
首页 > 其他分享> > 617. 合并二叉树

617. 合并二叉树

作者:互联网

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    if(root1==nullptr)return root2;
    if(root2==nullptr)return root1;
    TreeNode* meg=new TreeNode(root1->val+root2->val);
    meg->left=mergeTrees(root1->left,root2->left);
    meg->right=mergeTrees(root1->right,root2->right);
    return meg;

    }
};

标签:right,TreeNode,val,nullptr,合并,617,二叉树,root1,left
来源: https://www.cnblogs.com/fengmao31/p/15915797.html