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 ...
随机推荐
- About the diffrence of wait timed_wait and block in java
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * * @au ...
- Java Spring的简单见解
Spring的注解特性,IOC控制反转 首先了解依赖注入是什么,就是在实例化对象的时候并不需要每次都new对象出来,spring管理对象,在你配置been或者@service时候 Spring会自动帮 ...
- CSS背景效果
前面的话 本文将详细介绍CSS背景效果 条纹背景 [双条纹背景] background:linear-gradient(#fb3 50%, #58a 50%); background-size: 10 ...
- CCIE-MPLS基础篇-实验手册
又一部前期JUSTECH(南京捷式泰)工程师职业发展系列丛书完整拷贝. MPLS(Multi-Protocol Label Switching) 目录 1:MPLS 基础实验.... 1.1实验拓扑. ...
- JavaScript 的使用基础总结③
JavaScript 中的对象 JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... JavaScript 允许自定义对象. (一)数组 数组对象的作用是:使用单独的变量名来存储 ...
- 老板让我们去陪睡!-It高管的焦虑
老板是我非常敬重的前领导之一,他的一些管理风格,也影响了后来我对技术团队的管理.就是这样一个非常令人尊敬的领导,为什么会有这么过分的要求,请允许我先卖个关子,接下来就会知道. 理想企业 什么是程序员理 ...
- 个人总结-Alpha阶段
一.个人总结 经过几周的Alpha阶段,对于软件设计有了大概的认识,也深刻感觉到一款软件设计出来的不易,每款软件背后都是开发人员辛勤的汗水.在软件开发的过程中,也是会出现很多的问题,出现各种各样的bu ...
- 201521123017 《Java程序设计》第8周学习总结
1. 本周学习总结 2. 书面作业 Q1.List中指定元素的删除(题目4-1) 1.1 实验总结 for (int i = list.size()-1; i >=0; i--) {//从最后一 ...
- 201521123017 《Java程序设计》第6周学习总结
1. 本周学习总结 <> 2. 书面作业 Q1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 1. ...
- 201521123048 《Java程序设计》第1周学习总结
一 本周学习总结 第一周我们了解了java及其它的由来.刚开始学java显的特别吃力,对于一些概念和程序执行步骤什么的都不好理解,也有很多在编程时容易出错的地方需要花时间理解和记忆.初步一周下来,在我 ...