leetcode-hot100 2.合并二叉树 (cpp)
程序员文章站
2022-03-07 11:51:36
...
图片来自leetcode 截图
//树的先序遍历,分左右,先做操作,再递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (t1 == NULL)
return t2;
if (t2 == NULL)
return t1;
t1->val = t1->val + t2->val;
t1->left = mergeTrees(t1->left, t2->left);
t1->right = mergeTrees(t1->right, t2->right);
return t1;
}
};
上一篇: linux进程间通信的方式有哪些