617. Merge Two Binary Trees(合并二叉树)
来源:互联网 发布:网络游戏编程设计专业 编辑:程序博客网 时间:2024/05/18 00:37
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
Example 1:
Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/ \
4 5
/ \ \
5 4 7
Note: The merging process must start from the root nodes of both trees.
这道题给了我们两个二叉树,让我们合并成一个,规则是,都存在的结点,就将结点值加起来,否则空的位置就由另一个树的结点来代替。那么根据过往经验,处理二叉树问题的神器就是递归,那么我们来看递归函数如何去写。根据题目中的规则,我们知道如果要处理的相同位置上的两个结点都不存在的话,直接返回即可,如果t1存在,t2不存在,那么我们就以t1的结点值建立一个新结点,然后分别对t1的左右子结点和空结点调用递归函数,反之,如果t1不存在,t2存在,那么我们就以t2的结点值建立一个新结点,然后分别对t2的左右子结点和空结点调用递归函数。如果t1和t2都存在,那么我们就以t1和t2的结点值之和建立一个新结点,然后分别对t1的左右子结点和t2的左右子结点调用递归函数,参见代码如下:
解法一:
/** * 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) { TreeNode* ans = helper(t1, t2); return ans; } TreeNode* helper(TreeNode* n1, TreeNode* n2) { if(!n1 && !n2) return nullptr; TreeNode* tmp = new TreeNode(0); //创建新点,方法要正确 if(!n1 && n2) { tmp->val = n2->val; tmp->left = helper(nullptr, n2->left); tmp->right = helper(nullptr, n2->right); } else if(n1 && !n2) { tmp->val = n1->val; tmp->left = helper(n1->left, nullptr); tmp->right = helper(n1->right, nullptr); } else { tmp->val = n1->val + n2->val; tmp->left = helper(n1->left, n2->left); tmp->right = helper(n1->right, n2->right); } return tmp; }};
其实远不用写的像上面那么复杂,我们连额外的函数都不用写,直接递归调用给定的函数即可,我们首先判断,如果t1不存在,则直接返回t2,反之,如果t2不存在,则直接返回t1。如果上面两种情况都不满足,那么以t1和t2的结点值之和建立新结点t,然后对t1和t2的左子结点调用递归并赋给t的左子结点,再对t1和t2的右子结点调用递归并赋给t的右子结点,返回t结点即可,参见代码如下:
解法二:
class Solution {public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { if (!t1) return t2; if (!t2) return t1; TreeNode *t = new TreeNode(t1->val + t2->val); t->left = mergeTrees(t1->left, t2->left); t->right = mergeTrees(t1->right, t2->right); return t; }};
笔记:对树进行操作,递归是最常见的;学会使用new创建新类别。
- 617. Merge Two Binary Trees(二叉树的合并)
- 617. Merge Two Binary Trees(合并二叉树)
- 617. Merge Two Binary Trees 二叉树合并
- 【二叉树】617. Merge Two Binary Trees
- leetcode Merge Two Binary Trees 合并二叉树
- 【Leetcode】Merge Two Binary Trees 合并两个二叉树
- LeetCode 617 Merge Two Binary Trees(递归合并二叉树)
- LeetCode--Merge Two Binary Trees(合并两个二叉树)Python
- 617.Merge Two Binary Trees(合并两棵树)
- leetcode 617. Merge Two Binary Trees(C语言,二叉树求和,递归)53
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- 617. Merge Two Binary Trees
- php获取当前域名
- 【Java基础12_2】String类
- Jquery点击按钮弹出alert
- deeplearing.ai week3 学习笔记
- spring注解与xml同时使用:bean命名问题及冲突
- 617. Merge Two Binary Trees(合并二叉树)
- 基于Qt的收银点餐系统之UI的基本实现(一)
- python 类
- leetcode_606. Construct String from Binary Tree ? 待解决
- RESTful API 设计指南
- 用python resize 图片
- javascript关于图片加载的处理
- 1701H2 王建瑜 连续第五天
- 正确看待双十一电商潜在困局,智能化营销盘活预热期