欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

leetcode-hot100 2.合并二叉树 (cpp)

程序员文章站 2022-03-07 11:51:36
...

leetcode-hot100 2.合并二叉树 (cpp)
图片来自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;
	}
};
相关标签: leetcode-hot100