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 ...
随机推荐
- Weblogic wls RCE 漏洞验证POC
#!/usr/bin/env python # coding:utf-8 # @Date : 2017/12/22 17:11 # @File : weblogic_poc.py # @Author ...
- Class python31
# class Teacher: # def __init__(self, name, age, sex, salary, level): # self.name = name # self.age ...
- getParameter的用法及含义
equest.getparameter用来获取页面输入框输入的数据例如:jsp页面学员账户:<input type="text" name="username&qu ...
- 跨域Ajax原理以及浏览器同源策略
- VS项目模板文件位置
目录: D:\Users\lyn\Documents\Visual Studio 2012\Templates\ProjectTemplates 模板文件完整路径: D:\Users\lyn\Do ...
- js面试题知识点全解(一原型和原型链)
1.如何准确判断一个变量是数组类型2.写一个原型链继承的例子3.描述new一个对象的过程4.zepto(或其他框架)源码中如何使用原型链知识点:1.构造函数2.构造函数-扩展3.原型规则和示例4.原型 ...
- 使用foreach获取数据列表的全部信息
先把代码列出来:(在admin/listAdmin.php中) <?php foreach($rows as $row):?> //注意,这里的foreach($rows as $row) ...
- C++结构体的定义、初始化和引用
定义: 结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构. 声明一个结构体类型的形式是: struct Student{ //声明一个结构体类型Student in ...
- Spring 已看 没用
注解 @Autwired 依赖注入 作用: 自动按照类型注入.当使用注解注入属性时,set方法可以省略.它只能注入其他bean类型.当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在 ...
- JDK并发包2-线程池