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. IDEA配置Tomcat

    如何用IDEA写Servlet在我别的博文有! 注意:如果不能成功启动Tomcat,很有可能是JDK版本和Tomcat版本不匹配,此时你可以降低JDK版本试试

  2. python实例编写(4)--js,滚动条,cookie,验证码,获取特定属性的元素,实现原理

    一.调用js 执行方法:execute_script(script,*args) 场景一:在页面上直接执行调用js 场景二:在定位的某个元素上执行调用js 如:掩藏文字(提示插件 tooltip设置淡 ...

  3. java.sql.Exception:setString 只能处理少于 32766 个字符的字符串

    java.sql.Exception:setString 只能处理少于 32766 个字符的字符串 解决方式是 : 升级ojdbc的版本,   将原来的 ojdbc14_10.2.0.2.0.jar ...

  4. temp-成都农商行路径

    route add 30.3.4.0 mask 255.255.255.0 30.3.12.254 route add 30.3.12.0 mask 255.255.255.0 30.3.12.254 ...

  5. tomcat管理界面登录无法进入

    问题: 在打开tomcat界面之后,点击Manger App准备进入管理界面,路径:http://localhost:8080/manager/html. 输入正确的用户名和密码,但是出现401界面. ...

  6. MySQL_日期函数汇总

              如果转载,请注明博文来源: www.cnblogs.com/xinysu/   ,版权归 博客园 苏家小萝卜 所有.望各位支持!         关于MySQL日期时间函数,每回总 ...

  7. Check for Palindromes-FCC

    問題: 检查回文字符串 如果给定的字符串是回文,返回true,反之,返回false. 如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文). ...

  8. sqoop使用的问题

    找不到表 17/05/02 18:15:47 ERROR tool.ImportTool: Imported Failed: There is no column found in the targe ...

  9. Windows下 如何添加开机启动项

    Windows XP,Windows 7: 开始 ----> 所有程序 ----> 启动, 右键打开"启动"这个文件夹, 把想开机自动启动的软件快捷方式拖进去即可. ( ...

  10. 每周分享一 之 webSocket

    一:什么是webSocket ? webSocket是HTML5出的新协议,WebSocket协议支持,在受控环境中运行不受信任代码的客户端与选择了该代码通信的远程主机之间进行双向通信. 简单翻译一下 ...