LeetCode第617题:合并二叉树

问题描述

解题思路

略略略略略

C++代码

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

运行结果

posted @ 2019-01-02 15:59  朕蹲厕唱忐忑  阅读(162)  评论(0编辑  收藏  举报