//传入两个二叉树,用前序遍历,如果有相同节点则节点元素相加,如果没有相同节点,则用另一棵树
//的节点去补足该节点。
/**
* 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(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;
}
};
力扣-617.合并二叉树
最新推荐文章于 2024-08-29 16:23:51 发布