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. css把容器级别(div...)标签固定在一个位置(在页面最右边)

    .process{ border:1px solid #B7B7B8; background:#F8F8F8; width:80px; height:250px; <!--固定定位; text- ...

  2. shell重定向介绍及使用

    我们在写脚本或用脚本时,往往会发现 > /dev/null 2>&1 这类的命令.之前都是简单的了解一下,并没有深度消化,直到自己写脚本时,不认真写成了2>&1 &g ...

  3. [HBase_1] HBase安装与配置

    0. 说明 1. 简介 1.1 简介 基于 HDFS 的大表软件(实时数据库) 十亿行 x 百万列 x 上千个版本 版本是通过 mvcc 技术控制:multiple version concurren ...

  4. php中jpgraph库的使用

    用Jpgraph,只要了解它的一些内置函数,可以轻松得画出折线图.柱形图.饼状图等图表. 首先要保证PHP打开了Gd2的扩展: 打开PHP.ini,定位到extension=php_gd2.dll,把 ...

  5. TCP/IP协议族(笔记)

    1.HTTP HTTPS DCHP ICMP  SMTP IMAP MIME POP PPTP 协议族 tcp/ip 是基于tcp和ip这两个最初的协议之上的不同的通信协议的大集合 TCP/IP不是一 ...

  6. LeetCode算法题-Binary Watch(Java实现)

    这是悦乐书的第216次更新,第229篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第84题(顺位题号是401).二进制手表顶部有4个LED,代表小时(0-11),底部的6 ...

  7. Oauth2.0[笔记]

    背景 如果资源服务器只是提供资源给自己的应用,使用帐号密码做身份认证倒没什么问题,但如果需要提供资源给第三方应用,就会出现第三方应用需要与资源服务器共享身份凭证,这时会出现几个问题: 1.第三方应用需 ...

  8. PHP 缓存技术(一)

    移除光盘

  9. 7.01-beautiful_soup2

    # pip install beautifulsoup4 from bs4 import BeautifulSoup html_doc = """ <html> ...

  10. Python 的 GUI 开发工具

    kivy https://kivy.org/#home flexx https://flexx.readthedocs.io/en/stable/