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项目之苹果IAP内购JAVA服务器验证流程详解
1.前言 本博客是经历过多个项目检验的, 绝对真实, 适应于对苹果iap内购稍微有些了解的JAVA开发人员, 认真看, 定能完美解决苹果内购问题. 苹果IAP内购支付实际上是"将客户端支 ...
- centos7 克隆 网卡无法启用
1.克隆后查看网卡无法启用,报错信息如下: Apr :: agent systemd: network.service: control process exited, code=exited sta ...
- 关于collectionview布局的坑
不知道写了多少次collectionview,步了很多坑,现在看来虽然达到了自己想要的结果,却不知道其中所以然.还是总结一下,免得再走弯路: 场景是这样的,我要定制一个显示选择图片的排列,想要实现横向 ...
- 运算类实现 及 GNU Makefile基本结构
1.运算类的实现,代码如下: (1)operator.cpp #include<iostream> #include "operator.h" using names ...
- Zabbix4.0添加端口和进程监控
一:Zabbix设置主动模式: vim /etc/zabbix/zabbix_agent.conf Server=192.168.1.10 #被动模式的serverip地址,如果设置纯被动模式,可以注 ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
- 使用kafka和zookeeper 构建分布式编译环境
1:在每台机器上安装jdk, 脚本代码如下: 每一个机器上下载jdk,zookeeper,kafka 链接:https://www.oracle.com/technetwork/java/javase ...
- 点击li ,父辈出现; 子级,子辈不出现. prevUntil ---> 前面多个, 截止到 截止元素 , prev([expr]) --> 前面一个.
要求: 1. 点击第一级 [1知识点] 的时候, [1知识点] 前有 圆圈. 点击 第二级 [1-1知识点, 1-2知识点, 1-3知识点] 时 , [1知识点]出现 圆圈. 2. 点击 第一级 ...
- localStorage的使用记录
// 存数据 var str = JSON.stringify(back); localStorage.setItem("options", str); // 取数据 var op ...
- django重定向是如何实现的,用的什么状态码?
1,使用HTTPresponseredirect from django,http,import HttpResponseRedirect 2,使用redirct 和reverse 状态码:301和3 ...