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. ZooKeeper使用命令大全

    stat path [watch]    set path data [version]    ls path [watch]    delquota [-n|-b] path    ls2 path ...

  2. 关于Box Anemometer的安装配置遇到的几个坑

    Box Anemometer是一个非常不错的MySQL慢查询可视化工具平台,具体就不多详述了,官网是https://github.com/box/Anemometer 最近动自己动手搭了一个,具体搭建 ...

  3. 关于C#List中FindAll用法的一些简单示例

    using System; using System.Collections.Generic; public partial class List : System.Web.UI.Page { pro ...

  4. 轻量的web框架Bottle

    简洁的web框架Bottle 简介 Bottle是一个非常简洁,轻量web框架,与django形成鲜明的对比,它只由一个单文件组成,文件总共只有3700多行代码,依赖只有python标准库.但是麻雀虽 ...

  5. python 实现网页 自动登录

    完整代码: 1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from selenium import webdriv ...

  6. Unity 琐碎5 : 利用反射设置编辑器参数

    问题 最近处理unity资源打包问题时候经常遇到的一个问题就是平台切换和Bundle编译.一般情况下,平台转换我需要依赖Cache Serbver加快转换速度,但是在Build Bundle的时候我又 ...

  7. dep包安装与依赖库

    安装 点击下载 .deb 包:使用sudo dpkg -i xxx.deb 命令安装 依赖库问题 用sudo apt-get install -f解决依赖问题,解决后重新运行dpkg -i安装命令 验 ...

  8. js 文件引用传递参数

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code (function() {var hm = document.createElement(&q ...

  9. 【转】让Xcode支持iOS12.0 设备真机测试(不断更新真机支持包)Xcode 真机测试 iOS

    最新支持12.0 (16A366)! 所有真机包都是同一大版本号向下兼容.例如12.0 Beta6的真机包,是可以兼容12.0 Beta2及Beta1的. 12.0 (16A366)正式版可用!所有低 ...

  10. 【转】dos下 和 批处理中的 for 语句的基本用法

    for 语句的基本用法 : 最复杂的for 语句,也有其基本形态,它的模样是这样的:   在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for % ...