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.

/*  合并的树是新建结点
递归的思想,自己写不出来。。看discuss后觉得很简单,真尴尬,其实就是个二叉树的前序遍历,先处理根节点,然后递归处理两个左右子树。
*/ /**
* 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) {
if (t1 == NULL && t2 == NULL) return NULL;
TreeNode* root = new TreeNode((t1 != NULL ? t1 -> val: ) + (t2 != NULL ? t2 -> val: ) ); // 初始化根节点,根据t1和t2的存在情况
root -> left = mergeTrees((t1 != NULL ? t1 -> left : NULL), (t2 != NULL ? t2 -> left : NULL));
root -> right = mergeTrees((t1 != NULL ? t1 -> right : NULL), (t2 != NULL ? t2 -> right : NULL)); return root;
}
};

617. Merge Two Binary Trees(Easy)的更多相关文章

  1. 【Leetcode_easy】617. Merge Two Binary Trees

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

  2. 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 ...

  3. 17.Merge Two Binary Trees(合并两个二叉树)

    Level:   Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...

  4. LeetCode 617. Merge Two Binary Tree (合并两个二叉树)

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

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. [LeetCode&Python] Problem 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 ...

  9. [Algorithm] 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 ...

随机推荐

  1. MySQL 5.7开启二进制日志注意事项

    最近才开始将部分MySQL 5.6升级到MySQL 5.7, 在开启MySQL的二进制日志时,发现MySQL 5.7 与MySQL 5.6已有细微区别.如果在my.cnf配置文件中,只设置了全局系统变 ...

  2. PL/SQL重新编译包无反应案例2

    在这篇"PL/SQL重新编译包无反应"里面介绍了编译包无反应的情况,今天又遇到一起案例, 在测试环境中,一个包的STATUS为INVALID,重新编译时,一直处于编译状态,检查发现 ...

  3. [20181108]12c sqlplus rowfetch参数4.txt

    [20181108]12c sqlplus rowfetch参数4.txt --//12cR2 可以改变缺省rowfetch参数.11g之前缺省是1.通过一些测试说明问题.--//前几天做的测试有点乱 ...

  4. 【递推】ZSC1072: 数学、不容易系列之二—— LELE的RPG难题

    思路如下: f(n) = 1, ... , n-2 , n-1, n 前n-2个已涂好后,涂第n-1个即有2种情况: n-1的色与n-2和1的色都不相同,那么n就是剩下的那个色,没有选择. 即就是f( ...

  5. 【Teradata 】TD最大列数

    1.一个表最大列数限制是多少? DB2,表最大列数1012,视图最大列数5000:一行最大长度32677Byte Teradata 表最大列数和视图最大列数2048,:16版本前,一行最大长度为64k ...

  6. 设计模式のNullObjectPattern(空对象模式)----行为模式

    一.产生背景 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查.Null 对象不是检查空值,而是反应一个不做任何动作的关系.这样的 Null 对象也可 ...

  7. CentOS 7 上安装vim(默认未安装)

    今天使用CentOS 7,发现未安装vim,所以重新安装 执行命令: yum -y install vim* 然后就可以使用了

  8. Ubuntu开启或重启ssh服务

    开启ssh服务首先需要安装打开ssh服务的库: sudo apt-get install openssh-server 检查当前的ssh开启情况: ps -e |grep ssh 如果有sshd,则s ...

  9. 复习centos7命令---vim常用

    1.如果想向下移动5行:在一般模式下按:5↓: 2.如果想想左右移动5行:在一般模式下按:5← 3.移动到一行的开头:一般模式下按:0 4.移动到一行的末尾:一般模式下按:$ 5.移动到屏幕中的第一行 ...

  10. emqtt在centos6下的安装

    1 emqtt下载地址 http://www.emqtt.com/downloads 右键 复制链接 http://www.emqtt.com/downloads/3011/centos6 2 打开服 ...