前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode笔记:617. Merge Two Binary Trees

LeetCode笔记:617. Merge Two Binary Trees

作者头像
Cloudox
发布2021-11-23 16:35:01
2800
发布2021-11-23 16:35:01
举报
文章被收录于专栏:月亮与二进制

问题(Easy):

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:

Output: Merged tree:

Note: The merging process must start from the root nodes of both trees.

大意:

给出两个二叉树,想象用一个来覆盖另一个,两棵树中有些节点位置会重叠,有些不会。 你需要将它们合并到一个新二叉树。合并规则是如果两个节点重叠了,结果节点的值是两个节点值的和,如果没重叠,就取其中非null的节点作为新树的节点。 例1: 输入:

输出: 合并成的树:

注意:合并过程必须从两棵树的根节点开始。

思路:

从两个根节点开始,先判断根节点是否为空,都不为空则利用递归,将根节点的值相加,然后判断左右子节点是否分别为空,有一个为空则直接取另一个节点,都不为空则递归处理。

这里我们总是以第一颗树作为返回的新树,所以如果要相加节点值,则都加到第一颗树节点上,如果第二颗树的节点为null,则直接取第一颗树的节点,如果第一颗树的节点为null,则将第二颗树的节点复制到第一颗树来,需要注意的树不能直接让节点本身做赋值,要将节点赋值给父节点的子节点指针才算是建立了树关系。

代码(C++):

代码语言:javascript
复制
/**
 * 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:
    void merge(TreeNode *t1, TreeNode *t2) {
        t1->val += t2->val;
        
        if (t1->left == NULL) t1->left = t2->left;
        else 
            if (t2->left != NULL) merge(t1->left, t2->left);
        
        if (t1->right == NULL) t1->right = t2->right;
        else 
            if (t2->right != NULL) merge(t1->right, t2->right);
        
    }
    
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        if (t1 == NULL) return t2;
        if (t2 == NULL) return t1;
        
        merge(t1, t2);
        
        return t1;
    }
};

合集:https://github.com/Cloudox/LeetCode-Record

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/12/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题(Easy):
  • 大意:
  • 思路:
  • 代码(C++):
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档