刷题——合并二叉树

合并二叉树_牛客题霸_牛客网

方法一:

cpp 复制代码
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        // write code here
        if(t1 == NULL) return t2;
        if(t2 == NULL) return t1;
        TreeNode* head= new TreeNode(t1->val + t2->val);
        head->left = mergeTrees(t1->left, t2->left);
        head->right = mergeTrees(t1->right, t2->right);
        return head;
    }

方法二:

cpp 复制代码
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        // write code here
        if(t1 == NULL) return t2;
        if(t2 == NULL) return t1;
       t1->val+= t2->val;
       t1->left = mergeTrees(t1->left, t2->left);
       t1->right = mergeTrees(t1->right, t2->right);
        return t1;
    }
相关推荐
码途行者6 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
头发还在的女程序员6 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同
java·开发语言·充电桩系统·充电桩管理·充电系统·充电小程序
timeDifference₇8 小时前
Python Vibe Coding实操实验报告
开发语言·jvm·python
geovindu9 小时前
python:DecisionTree
开发语言·后端·python·算法
geovindu10 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
炸膛坦客14 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
夜不会漫长14 小时前
C++:C++11特性
开发语言·c++·算法
apcipot_rain15 小时前
C++转java日记(13)
java·开发语言
郝学胜-神的一滴16 小时前
Effective Python 条款41:试Mix‑In组合式开发
开发语言·pytorch·python·pycharm·numpy
吃我一窝jio16 小时前
Python与Redis集合深度解析
开发语言·redis·笔记·python