给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

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) {
if (t1 == NULL && t2 == NULL)
return NULL;
if (t1 == NULL)
{
t2->left = mergeTrees(t1, t2->left);
t2 ->right = mergeTrees(t1, t2->right);
return t2;
}
else if (t2 == NULL)
{
t1 ->left = mergeTrees(t1 ->left, t2);
t1 ->right = mergeTrees(t1 ->right, t2);
return t1;
}
else
{
t1->val = t1->val + t2->val;
t1 ->left = mergeTrees(t1->left, t2->left);
t1 ->right = mergeTrees(t1->right, t2->right);
return t1;
}
}
};

Leetcode617.Merge Two Binary Trees合并二叉树的更多相关文章

  1. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  2. LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

    题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

  3. [LeetCode] 617. Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  4. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

  5. leetcode617 Merge Two Binary Trees

    """ Given two binary trees and imagine that when you put one of them to cover the oth ...

  6. LeetCode 617. 合并二叉树(Merge Two Binary Trees)

    617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...

  7. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  8. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

  9. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

随机推荐

  1. vue基础知识总结

    vue不支持安卓6.0以下版本,切记!!! 1.创建vue实例 var vm = new Vue({ el: '#app', //所指向的dom的id data:{ }, //与dom元素绑定的数据 ...

  2. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  3. css3中 百分比宽度减去固定宽度的写法

    div{ /*实现了宽度为父容器宽度减去固定的300像素*/ width:-webkit-calc(100% - 300px); width:-moz-calc(100% - 300px); widt ...

  4. Drupal创建Omega 4.x 子主题layout笔记

    Adding a new region to your Omega 4.0 subtheme (Drupal) Drupal: Creating a custom layout with Omega ...

  5. Linux产生coredump文件(core)

    1.可以使用命令 ulimit -c unlimited 来开启 core dump 功能,并且不限制 core dump 文件的大小: 如果需要限制文件的大小,将 unlimited 改成你想生成 ...

  6. LUOGU P2280 [HNOI2003]激光炸弹

    传送门 解题思路 二维前缀和. 代码 #include<iostream> #include<cstdio> #include<cstring> using nam ...

  7. QEventLoop配合QTimer实现阻塞任务超时处理

    A阻塞主线程正常运行,需要做特殊处理. 以下代码可实现,A阻塞或者正常处理时,均不阻塞主线程正常处理. QEventLoop eventloop; // use point to manage eve ...

  8. kill 3000

    杀3000端口,是作为一个web未开发人员经常遇到的事情 所以我今天就分享一下我的杀3000端口秘诀 lsof -i: 先要找到端口 node zcool 20u IPv6 0xdddbb4f6f12 ...

  9. 工控安全入门(七)—— plc的网络

    上一篇我们详细分析了bootram和Vxworks的基本启动流程,这篇文章中我们把视线转到plc的网络部分,同时来复现我们第一个.第二个工控安全漏洞. VxWorks的网络设备驱动 一般我们说有三种设 ...

  10. 469 Same Tree

    原题网址:https://www.lintcode.com/problem/same-tree/description 描述 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构, ...