题目

https://leetcode-cn.com/problems/merge-two-binary-trees/

题解

测试用例

输入:
[1,3,2,5]
[2,1,3,null,4,null,7]
[1]
[1,2]
[1,null,2,null,3,null,4]
[1,2,null,3,null,4,null,5]
答案:
[3,4,5,5,4,null,7]
[2,2]
[2,2,2,3,null,null,3,4,null,null,4,5]

思路

同时遍历左右两棵二叉树,把右边的树上的值加到左边来。

这样做有一个不严谨的地方,是改变了原有二叉树的值,而题目中要求返回一个 new tree。

public static TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if (root1 != null && root2 != null) {root1.val = root1.val + root2.val; // already ensure both nodes are not nullif (root1.left != null && root2.left != null)mergeTrees(root1.left, root2.left);if (root1.right != null && root2.right != null)mergeTrees(root1.right, root2.right);if (root1.left == null)root1.left = root2.left;if (root1.right == null)root1.right = root2.right;} else if (root1 == null) {root1 = root2;}return root1;
}

leetcode 617. Merge Two Binary Trees | 617. 合并二叉树(Java)相关推荐

  1. 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 ...

  2. 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 ...

  3. [leetcode] 617. Merge Two Binary Trees

    题目描述 给定两棵树,要求合并它们,如果节点重合则新合并的树的节点为它们之和,否则不变. 思路 简单递归,c语言怎么创建新节点忘记了,就直接写的. 代码 /*** Definition for a b ...

  4. 【Tree-easy】617. Merge Two Binary Trees 合并两个二叉树

    1. 题目原址 https://leetcode.com/problems/merge-two-binary-trees/ 2. 题目描述 3. 题目大意 给定两个二叉树,现在要求将这两个二叉树合并, ...

  5. 《LeetCode力扣练习》第617题 合并二叉树 Java

    <LeetCode力扣练习>第617题 合并二叉树 Java 一.资源 题目: 给你两棵二叉树: root1 和 root2 . 想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些 ...

  6. C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...

  7. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

  8. 3/100. Merge Two Binary Trees

    将两个二叉树相同位置的数值相加,相加的方法一样,则使用递归法挨个相加即可. # Definition for a binary tree node. # class TreeNode: # def _ ...

  9. leetcode 1768. Merge Strings Alternately(交替合并字符串)

    把word1和word2中的字母交替地整合成一个字符串. 思路: 很简单,主要是看什么时候放word1, 什么时候放word2, 可用一个0,1决定,每次异或1. public String merg ...

最新文章

  1. Android性能优化
  2. MATLAB 数据分析方法(第2版)1.3 MATLAB基本语法
  3. 树莓派超声波模块测距
  4. java 将base64字符串转换成图片
  5. 【哈工大SCIR Lab】Attention!注意力机制可解释吗?
  6. java正则表达式http_Java 正则表达式(精华)
  7. 沟通CTBS金融行业远程接入解决方案
  8. 什么是磁盘?磁盘的组成?接口和分区?
  9. Ardupilot Pre-Arm安全检查程序分析
  10. connect: cannot assign requested address
  11. Windows下的pixhawk环境搭建
  12. Matlab中axis函数用法总结
  13. 问卷星复制内容方法(利用官方功能导出为word格式)
  14. 查询速度慢的原因查找
  15. Excel如何快速批量生成指定性别的随机姓名
  16. 独立性权重指标怎么计算?
  17. 本科毕业设计格式规范
  18. 计算机用户的文件夹路径是什么,移动Win7用户文件夹(我的文档)默认位置至D盘...
  19. 笔记本的构造介绍--处理器
  20. 神经网络模型大小怎么看,神经网络模型大小计算

热门文章

  1. html css 画五角星,纯 CSS3 绘制图形(心形、五角星、六边形等)
  2. HDU4604(双端队列与DP)
  3. 2.static关键字.rs
  4. OD+IDA6.1破解HideWizardv9.29(无忧隐藏)
  5. WinSock完成端口I/O模型
  6. 手把手教你玩转ARP包(四)
  7. vc得到屏幕的当前分辨率方法
  8. 私有化仓库的 GO 模块使用实践
  9. SQL 学习最强刷题网站!
  10. 低学历程序员的红利来了,这个政策来的太惊喜!