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 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 (合并两个二叉树)的更多相关文章
- [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 ...
- 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 ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 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 ...
- 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 ...
- [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 ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
随机推荐
- Java+大数据开发——HDFS详解
1. HDFS 介绍 • 什么是HDFS 首先,它是一个文件系统,用于存储文件,通过统一的命名空间--目录树来定位文件. 其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角 ...
- HTTP第一篇
为什么要学HTTP? 我们绝大多数的Web应用都是基于HTTP来进行开发的.我们对Web的操作都是通过HTTP协议来进行传输数据的. HTTP的诞生主要是为了能够让文档之间相互关联,形成超文本可以互相 ...
- Mysql修改id自增值
如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数 truncate table 表名 如果想保留之前的记录,从某一id(3356)重新开始 alter table 表名 ...
- redis 基础学习总结
背景:因为项目用到了redis,加上之前一直听说了redis,但一直没有用过,正好项目现在要用到了,抽时间简单学习了下,做个记录总结下. 一 .Redis简介 介绍Redis之前,先了解下NoSQL ...
- eclipse配置maven + 创建maven项目(三)
上篇博文中我们介绍了maven下载.安装和配置(二),这篇博文我们配置一下eclipse,将它和maven结合,并我们创建一个maven的项目. 准备工作 在eclipse配置maven之前需要我们做 ...
- 根据HttpServletRequest获取用户真实IP地址
原因: 当我们通过request获取客户端IP时,自身服务器通常会为了保护信息或者负载均衡的目的,对自身服务器做反向代理.此时如果我们通过request.getRemoteAddr();可能获取到的是 ...
- FS BPM 业余研发(用户详细操作手册--单人串行/并行)之 深圳分公司技术部请假审批流程
1.FS BPM 简介 BPM软件中BPM是英文字母缩写,大致有二个意思.第一.Business Process Management,即业务流程管理,是一套达成企业各种业 务环节整合的全面管理模式. ...
- Day3 文件操作和函数
一 文件操作 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1.1打开文件读取内容 print(open("sounds","r", ...
- Spring Boot 学习之项目构建
最近做了外包,都是工程专业术语,前期熟悉项目看文档看的挺累的,闲暇时间自己学习一下Spring Cloud,找点乐趣. 就有了下面的小项目. 本项目是一个Spring boot项目. 一.nginx做 ...
- python 脚本开发实战-当当亚马逊图书采集器转淘宝数据包
开发环境python2.7.9 os:win-xp exe打包工具pyinstaller 界面tkinter ============================================= ...