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 ...
随机推荐
- Python类(四)-多态
多态即一个接口,多种实现 按照平常直接调用 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obje ...
- Solaris11 How-To
允许root用户使用ftp - 修改/etc/ftpd/ftpusers文件,移除或注释掉"root" - 修改/etc/proftpd.conf文件,"RootLogi ...
- c#抓取网页数据
写了一个简单的抓取网页数据的小例子,代码如下: //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strRe ...
- ORA-00904: 标识符无效——解决方案
转自:https://blog.csdn.net/jajavaja/article/details/49122639 建表时列名用双引号引着(用Navicat工具建表默认是加上双引号的),java连接 ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- IDEA中Git实战
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...
- Python 黑客 --- 001 UNIX口令破解机
Python 黑客 实战:UNIX口令破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V crypt 库是Python内置的库.在UNIX系统使 ...
- Boost 线程学习笔记
Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...
- ae中用粒子系统做的特效怎么循环
- google的protocol buffers 对象的序列化 for java
前言: protobuf确实比JSON快很多倍,看下面的图就知道了. 环境: win7 x64 eclipse 4.3 protoc-2.5.0 安装包下载: https://code.google. ...