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.


题目标题:Tree

  这道题目给了我们两个二叉树,要我们合并两个二叉树,合并的树的每一个点的值等于两个二叉树相对位置的点的值的合。利用recursively call来实现,我们来分析一下。对于每一个新的点的值,我们需要做的就是把两个树中的同样位置的点的值相加。然后recursively来继续代入mergeTrees,左边的点,就代入同样位置两个点的左边。右边的点就代入同样位置的两个点的右边,直到代入得两个点都是null,就停止代入,return回去。 那么对于每一个新的点,有三种情况:1- 两个点都是null,就直接return; 2- 两个点都不是null,直接相加;3- 两个点其中有一个点是null,那么就取另外一个点的值。 需要注意的是,对于每一个新的点,如果代入的两个点其中一个是null的话,那么这个null的点的 .left 和.right 是error。所以要先initial 一下。

Java Solution:

Runtime beats 60.24%

完成日期:06/29/2017

关键词:Tree

关键点:利用recursively来求每一个新的点,以及这个点的左右child

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public TreeNode mergeTrees(TreeNode t1, TreeNode t2)
{
TreeNode root;
TreeNode left_1 = null, left_2 = null;
TreeNode right_1 = null, right_2 = null; if(t1 == null && t2 == null)
return null;
else if(t1 != null && t2 != null)
{
root = new TreeNode(t1.val + t2.val);
left_1 = t1.left;
left_2 = t2.left;
right_1 = t1.right;
right_2 = t2.right;
}
else if(t1 != null && t2 == null)
{
root = new TreeNode(t1.val);
left_1 = t1.left;
right_1 = t1.right;
}
else
{
root = new TreeNode(t2.val);
left_2 = t2.left;
right_2 = t2.right;
} root.left = mergeTrees(left_1, left_2);
root.right = mergeTrees(right_1, right_2); return root;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 617. Merge Two Binary Tree (合并两个二叉树)的更多相关文章

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

  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#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

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

  5. Leetcode 617 Merge Two Binary Trees 二叉树

    题意: 给定两棵树,将两棵树合并成一颗树 输入 Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出 合并的树 3 / \ 4 5 / \ \ 5 4 7 ...

  6. [leetcode]21. Merge Two Sorted Lists合并两个链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  8. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  9. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

随机推荐

  1. 零基础的人该怎么学习JAVA

    对于JAVA有所兴趣但又是零基础的人,该如何学习JAVA呢?对于想要学习开发技术的学子来说找到一个合适自己的培训机构是非常难的事情,在选择的过程中总是  因为这样或那样的问题让你犹豫不决,阻碍你前进的 ...

  2. JAVA基础---编码解码

    所谓编码 即char->byte 所谓解码 即byte->char ISO-8859-1 中文字符会被黑洞吸收 全部变为"?" GB2312 汉字可以被编码为双字节 但 ...

  3. Quartz学习——Spring和Quartz集成详解(三)

    Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...

  4. 第6章 Overlapped I/O, 在你身后变戏法 ---Win32 文件操作函数 -2

    Win32 之中有三个基本的函数用来执行 I/O,它们是:        i CreateFile()        i ReadFile()        i WriteFile()    没有另外 ...

  5. Linux入门之常用命令(9)进程及端口查看

    [Linux下查看进程] 查看程序对应进程号:ps –ef|grep 程序名 查看进程占用端口:ss -pl | grep 进程号 [通过进程查看端口] 查看占用的端口号:netstat –nltp| ...

  6. Dynamic Inversions II 逆序数的性质 树状数组求逆序数

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  7. 【bzoj1103】【POI2007】【大都市】(树状数组+差分)

    在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n的n个小村庄,某些村庄之 ...

  8. Pycharm安装、设置、优化

    一.版本选择 建议安装5.0版本,因为好注册,这个你懂得. 下载地址: https://confluence.jetbrains.com/display/PYH/Previous+PyCharm+Re ...

  9. ImageSharp一个专注于NetCore平台图像处理的开源项目

    今天大家分享的是一个专注于NetCore平台图像处理的开源项目,老实说为这篇文章取名字想了5分钟,可能是词穷亦或是想更好的表达出这款开源项目的作用:这个项目在图像处理方面有很多功能,如:缩放,裁剪,绘 ...

  10. 在C#中实现串口通信的方法

    通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册,不在本文讨论范围.可以访问h ...