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 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.
这道题给了我们两个二叉树,让我们合并成一个,规则是,都存在的结点,就将结点值加起来,否则空的位置就由另一个树的结点来代替。那么根据过往经验,处理二叉树问题的神器就是递归,那么我们来看递归函数如何去写。根据题目中的规则,我们知道如果要处理的相同位置上的两个结点都不存在的话,直接返回即可,如果t1存在,t2不存在,那么我们就以t1的结点值建立一个新结点,然后分别对t1的左右子结点和空结点调用递归函数,反之,如果t1不存在,t2存在,那么我们就以t2的结点值建立一个新结点,然后分别对t2的左右子结点和空结点调用递归函数。如果t1和t2都存在,那么我们就以t1和t2的结点值之和建立一个新结点,然后分别对t1的左右子结点和t2的左右子结点调用递归函数,参见代码如下
/**
* 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;
}
return helper(t1, t2);
}
private TreeNode helper(TreeNode t1, TreeNode t2){
if(t1 == null && t2 == null){
return null;
}
TreeNode root = null;
if(t1 != null && t2 == null){
root = new TreeNode(t1.val);
root.left = helper(t1.left, null);
root.right = helper(t1.right, null);
}
else if(t1 == null && t2 != null){
root = new TreeNode(t2.val);
root.left = helper(t2.left, null);
root.right = helper(t2.right, null);
}
else{
root = new TreeNode(t2.val+t1.val);
root.left = helper(t1.left, t2.left);
root.right = helper(t1.right, t2.right);
}
return root;
} }
LeetCode - 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)
617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- 【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] 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 ...
- 【LeetCode】617. Merge Two Binary Trees 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 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 ...
随机推荐
- Java反射《三》获取属性
package com.study.reflect; import java.lang.reflect.Field; /** * 反射,获取属性 * @ClassName: FieldDemo * @ ...
- gitblit系列七:使用Jenkins配置自动化持续集成构建
1.安装 方法一: 下载jenkin.exe安装文件 下载地址:https://jenkins.io/content/thank-you-downloading-windows-installer/ ...
- 【腾讯开源】前端预处理器语言图形编译工具 Koala使用指南
摘要:Koala是一款预处理器语言图形编译工具,支持Less.Sass.CoffeeScript.Compass框架的即时编译.无需手动输入命令去编译,后台监听文件是否有改变,如有修改会自动进行编译, ...
- Linux:【解决】无法连接 MKS:套接字连接尝试次数太多正在放弃
[解决]无法连接 MKS:套接字连接尝试次数太多正在放弃 操作: 我的电脑 -> 右键 -> 管理 -> 服务和应用程序 -> 服务: 开启下面的服务: 服务启动成功后,重 ...
- kbmMWLog输出日志到控制台或指定Grid
刚看到有人在kbmMW News问,有没有简单的方法,输出日志到Console窗口或者一个实际的Grid? 作者对此做回复,大意是这样: 对于输出日志到一个Memo,使用TkbmMWStringsLo ...
- python day03--字符串
一.字符串 1.索引 s1 = "python最牛B" S1[0]第0个,从零开始算 s1[8]“B” 2.切片 语法: str[start: end]规则: 顾头不顾腚, 从st ...
- Bug01_MyBatis_不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
xml 文件格式不正确.一般是开头约束不对. 我出现的问题是:<?xml version="1.0" encoding="UTF-8"?>写了两遍, ...
- 【Python】多进程-队列
#练习:队列 from multiprocessing import Process, Queue def offer(queue): # 入队列 queue.put("Hello Worl ...
- Tail Recusive
1.尾递归 double f(double guess){ if (isGoodEnough(guess)) return guess; else return f(improve(guess)); ...
- [翻译]60,000毫秒内对Linux进行性能诊断
原文链接:http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html 原文作者:Brendan Gregg,L ...