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 ...
随机推荐
- docker 端口映射iptables: No chain/target/match by that name错误解决方法
pkill docker iptables -t nat -F ifconfig docker0 down brctl delbr docker0 service docker restart
- 空中楼阁 ( House )最短路
题目描述: 话说Z4阴差阳错地来到了神秘岛.不久,他们发现,这是一个由n个小岛和一个中心岛组成的群岛,群岛之间有m座桥.令他们感到惊讶的是,这些桥并不是固定不变的,经较长时间的观察,发现它们会随时间作 ...
- java反射专题二
一丶Class中常用方法详解 1)getFields() 只能获取到运行时类中及其父类中声明为public的属性 2)getDeclaredFields() 获取运行时类本身声明的所有属性 3)get ...
- LNMP 1.5 php-fpm配置文件
php-fpm配置文件: /usr/local/php/etc/php-fpm.conf :php-fpm服务的配置文件 /usr/local/php/etc/php.ini :ph ...
- VS2015 MSVC编译FFMPEG
1.下载安装msys2 http://www.msys2.org/下载msys2 下载安装完成后,在msys2的shell中安装编译FFMPEG必要的命令行工具 pacman -S make gcc ...
- C语言学习笔记--C语言中的宏定义
1. C 语言中的宏定义 (1)#define 是预处理器处理的单元实体之一(因此,预处理器只是简单的进行替换,并不(2)#define 定义的宏可以出现在程序的任意位置(包括函数体的内部)(3)#d ...
- spring整合mybatis的事物管理配置
一.基本配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
执行Hive查询: Console是这样报错的 java.sql.SQLException: Error from org.apache.hadoop.hive.ql.exec.mr.MapRedTa ...
- Python之整数,浮点数和布尔类型
整数和浮点数有那个四则运算: 两种类型的数可以直接进行加减,当整数和浮点数进行加减的时候,结果会自动的变为浮点数,其中除法运算是“/”来表示的, 而余数的算术符号是“%”来表示的.如果是为了求除完后的 ...
- 使用LaTeX按IEEE模板写论文时的参考文献管理方法(BibTeX使用小结)
之前用LaTeX写论文时,参考文献都是手动添加管理的,真是让人很抓狂.所以这次趁着假期,简单看了一下怎么使用BibTeX对参考文献进行管理,这里以IEEE的最新模板为例. 首先说明,我之前用的是MiK ...