Merge Two Binary Trees——LeetCode进阶路
原题链接https://leetcode.com/problems/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.
思路分析
从两个树的根节点开始,对应节点相加……很朴素的一题了
源码附录
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;v1.
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1==null && t2==null)
{
return null;
}
if(t1==null || t2==null)
{
return t1==null ? t2 :t1;
}
t1.val = 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进阶路的更多相关文章
- 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] 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 合并二叉树
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 - 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 ...
随机推荐
- springboot+vue项目:工具箱
常用账号管理:工作相关账号.游戏账号.各平台账号 加班调休管理:公司没有对应的系统,需要自己记录加班调休情况. 待办事项:方便记录待办,以提醒还有哪些事情没有办理. 待实现功能: 1.点击侧边栏菜单, ...
- SSM:Spring整合Mybatis时,连接池和SQLSessionFactory的联系!
- C# 网页截图全攻略:三种技术与 Chrome 路径查找指南
全局配置 string url = "https://blog.csdn.net/sunshineGGB/article/details/122316754"; 一.Puppete ...
- [译] DeepSeek开源smallpond开启DuckDB分布式之旅
DeepSeek 正通过 smallpond(一种新的.简单的分布式计算方法)推动 DuckDB 超越其单节点的局限.然而,我们也需要探讨,解决了横向扩展的挑战后,会不会是带来新的权衡问题呢? 译者序 ...
- Java 解析XML的几种方式:DOM、SAX、JDOM和DOM4J。
归纳总结Java解析XML主要有四中方式,分别是DOM.SAX.JDOM和DOM4J.其中DOM和SAX是官方包自带,另外两个JDOM和DOM4J是第三方包. 一.此篇测试代码用到的XML情况 . 1 ...
- CRYPTO-DSA
CRYPTO-DSA 参考某位大佬的博客和nss的一些题目,这两天的DSA题目 DSA数字签名 | DexterJie'Blog [NCTF 2021]dsa task.py from Crypto. ...
- PostgreSQL下载
官方下载地址: https://www.postgresql.org/download/pgAdmin4客户端工具下载地址:https://www.pgadmin.org/download/pgadm ...
- bug|项目经验|记录某次页面div使用v-html标签渲染图片等内容的过程
前言 记录某次页面div使用v-html标签渲染图片等内容的过程 一.结论: get请求但被设置Sec-Fetch-*请求头的图片无法展示. 二.原因: 1.本项目中的img标签发起get请求,目标链 ...
- Obsidian 笔记一键转换发布为 Jekyll 博客
Obsidian 是一款功能强大且灵活的知识管理和笔记软件,与 Jekyll 这一轻量级静态博客框架的结合,既能保留 Obsidian 的网状知识关联优势,又能借助 Jekyll 的高效编译能力快速生 ...
- Docker,vs2019下 使用.net core创建docker镜像 遇到的一些问题
步骤主要分为以下几步: 1.创建docker for linux 的.netcore 项目(vs 自动创建了dockerfile 如果没有需要自己创建在根目录下) 2.编译项目到指定目录下 3.b ...