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

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

  1. struct TreeNode {
  2. int val;
  3. TreeNode *left;
  4. TreeNode *right;
  5. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  6. };
  7. class Solution {
  8. public:
  9. TreeNode * mergeTrees(TreeNode* t1, TreeNode* t2) {
  10. if (t1 == NULL && t2 == NULL)
  11. return NULL;
  12. if (t1 == NULL)
  13. {
  14. t2->left = mergeTrees(t1, t2->left);
  15. t2 ->right = mergeTrees(t1, t2->right);
  16. return t2;
  17. }
  18. else if (t2 == NULL)
  19. {
  20. t1 ->left = mergeTrees(t1 ->left, t2);
  21. t1 ->right = mergeTrees(t1 ->right, t2);
  22. return t1;
  23. }
  24. else
  25. {
  26. t1->val = t1->val + t2->val;
  27. t1 ->left = mergeTrees(t1->left, t2->left);
  28. t1 ->right = mergeTrees(t1->right, t2->right);
  29. return t1;
  30. }
  31. }
  32. };

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. Mysql 1864 主从错误解决方法

    故障描述: 在mysql 主库上增加了一个主键操作,没过5分钟就接受到zabbix报警mysql主从同步异常停止信息,一首凉凉送给自己.... 查看现在主从状态 (root@192.168.1.2) ...

  2. UTF小记(一)

    前言 十六进制(简写为hex或下标16)在数学中是一种逢16进1的进位制.一般用数字0到9和字母A到F(或a~f)表示,其中:A~F表示10~15,这些称作十六进制数字. 不同电脑系统.编程语言对于1 ...

  3. AFO之后……

    先上游记:@ 据说有一种东西叫狗屁不通文章生成器:@

  4. Python学习day06-Python基础(4)流程控制之while和for循环

    Python学习day06-流程控制之while和for循环 Python学习day06-流程控制之while和for循环while循环1. 语法2. while+break,while+contin ...

  5. ValueError: Variable conv1/weights already exists.

    跑TensorFlow程序的过程中出现了错误,解决之后再次跑时,报如下错误: ValueError: Variable conv1/weights already exists, 原因: 这是因为我在 ...

  6. reac-native + typescript 的环境搭建

    一. RN-TS环境搭建 . 安装RN脚手架 yarn add create-react-native-app -g yarn global add typescript . 创建项目文件夹 crea ...

  7. java笔试之求int型正整数在内存中存储时1的个数

    输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 关键点:n与二进制的1相与:判断最末位是否为1:向右移位. 类似题目是查找输入整数二进制中1的个数. package test; ...

  8. spark生态圈简介

    原文引自:http://www.cnblogs.com/shishanyuan/p/4700615.html 1.简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algori ...

  9. HBase访问接口

  10. 牛客网暑期ACM多校训练营(第一场)菜鸟补题QAQ

    签到题 J Different Integers(树状数组) 题目大意:给一个长为n的数组,每一个询问给两个数字i, j ,询问1~i, j~n这两个区间中有多少不同的数字,真的像是莫队裸题,但是两个 ...