Merge Two Binary Trees
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.
思路: 左右两棵树同时进行递归访问(根左右),若节点都存在,返回和,若某一棵树的当前节点不存在,则返回另一棵树的当前节点。
JAVA CODE
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1==null) return t2;
if(t2==null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left,t2.left);
// 递归,访问顺序:根左右
t1.right = mergeTrees(t1.right,t2.right);
return t1;
}
}
Merge Two Binary Trees的更多相关文章
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- LeetCode 617. 合并二叉树(Merge Two Binary Trees)
617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
- [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 ...
- [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 ...
- 617. Merge Two Binary Trees(Easy)
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 解题报告
题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- 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 ...
随机推荐
- jenkins 每个月1号到7号 一天执行一次
在线Crontab表达式执行时间验证 / crontab执行时间计算 - aTool在线工具验证 http://www.atool.org/crontab.php 1.Build periodic a ...
- spring 发邮件
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt116 Spring邮件抽象层的主要包为org.springframework ...
- [转]Java se 7 最新特性研究(一)
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp81 从2006到现在等待了多年的jdk7终于发布了.这里将对它的一些 ...
- HashMap 底层算法分析
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp72 Hash算法HashMap使用Hash算法,所以在解剖HashMap ...
- JavaScript--我发现,原来你是这样的JS(基础概念--灵魂篇,一起来学js吧)
介绍 这是红宝书(JavaScript高级程序设计 3版)的读书笔记第三篇(灵魂篇介绍),有着剩下的第三章的知识内容,当然其中还有我个人的理解.红宝书这本书可以说是难啃的,要看完不容易,挺厚的,要看懂 ...
- Flask05 cookie
1 什么是cookie 就是网站存放到你浏览器中的一部分固定内容:当你下次访问我这个网站的时候,你会把之前我存放到你浏览器中的数据带回来给我 你要先登录(用户名.密码) -> ...
- JS学习二(循环)
JS中的循环结构 [循环结构的执行步骤] 1.声明循环变量: 2.判断循环条件: 3.执行循环体操作: 4.更新循环变量: 然后,循环执行2~4,知道条件不成立.跳出循环. [while 循环] wh ...
- 团队作业4——第一次项目冲刺(Alpha版本) 1
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.完页面的设计 采用gui页面,现在也是最初的页面设计 2.完成接口的定义 与组员共同定义了接口 四.困难与问题 此次主要利 ...
- 201521123006 《java程序设计》 第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 class ArrayAlg { public static < ...
- 201521123030 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4 ...