17.Merge Two Binary Trees(合并两个二叉树)
Level:
Easy
题目描述:
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.
思路分析:
先序遍历两个二叉树,在遍历的过程中更新每个节点的值。
代码:
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}
public class Solution{
public TreeNode mergeBinaryTree(TreeNode t1,TreeNode t2){
if(t1==null&&t2==null)
return null;
else if(t1==null)
return t2;
else if(t2==null)
return t1;
else{
t1.val=t1.val+t2.val;
t1.left=mergeBinaryTree(t1.left,t2.left);
t1.right=mergeBinaryTree(t1.right,t2.right);
return t1;
}
}
}
17.Merge Two Binary Trees(合并两个二叉树)的更多相关文章
- [LeetCode] 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] 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 ...
- 17. Merge Two Binary Trees 融合二叉树
[抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...
- Leetcode617.Merge Two Binary Trees合并二叉树
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...
- 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 ...
- [Swift]LeetCode617. 合并二叉树 | 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)
617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
随机推荐
- ARRINC424—MORA(GRID)格式
每一整数经.纬度为一格,每格MORA值3位数字,表示百英尺.无法获知MORA值得网格一UNK表示. 经纬网格起始点坐标,每个网格从左下角开始计数,每经纬度一度切分一个网格.每行数据代表某一维度上往东或 ...
- Drools学习笔记4—Consequence/RHS
Right Hand Side,当LHS所有条件满足才会执行 可以使用LHS部分定义的绑定变量.全局变量.或者直接编写JAVA代码. 提供宏函数操作working memory fact对象,如ins ...
- Nginx简单入门教学,包学包会,让你不再依赖伪大神!
这篇教程简单介绍了 nginx 并且讲解了一些 nginx 可以解决的简单任务.这里,我们假设 nginx 已经安装在读者的机器上.如果没有,可以看一下如何安装 nginx.这篇教程主要讲解的是如果启 ...
- xcopy 命令行
https://www.cnblogs.com/yang-hao/p/6003308.html xcopy-参数详解 XCOPY——目录复制命令 1.功能:复制指定的目录和目录下的所有文件连同目 ...
- 部署和调优 2.4 tomcat安装
下载tamcet 官网 http://tomcat.apache.org/ 左侧选择版本 复制下载链接 切换到下载目录 cd /usr/local/src linux wget wget http:/ ...
- JavaSwing文件选择器 JFileChooser的使用
先看效果吧! 说明:选择文件或者文件夹.本例子就直接在控制台输出文件或者文件夹的路径.实际开发中,就可以将文件或文件夹的路径封装为File的实例来使用了. package test; import j ...
- springmvc配置式开发下的视图解析器
多个视图解析器优先级:
- Django扩展Auth-User表的几种方法
方式1, OneToOneField from django.contrib.auth.models import Userclass UserProfile(models.Model): user ...
- 导出Excel解决方案之一NOPI
一.概要 导出Excel这个功能相信很多人都做过,但是实现这个功能解决方案有好几种,今天我未大家介绍一种比较新的,其实也不新了- -!它叫NPOI,可以完美操作EXCEl的导入和导出操作,让我们一起看 ...
- GPG入门
GPG入门 摘自https://www.jianshu.com/p/1257dbf3ed8e Nitroethane 关注 2016.07.19 17:36* 字数 1003 阅读 6560评 ...