当前位置: 首页 > news >正文

17.Merge Two Binary Trees(合并两个二叉树)

Level:

  Easy

题目描述:

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.

思路分析:

  先序遍历两个二叉树,在遍历的过程中更新每个节点的值。

代码:

public class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int x){
        val=x;
    }
}
public  class Solution{
    public TreeNode mergeBinaryTree(TreeNode t1,TreeNode t2){
        if(t1==null&&t2==null)
            return null;
        else if(t1==null)
            return t2;
        else if(t2==null)
            return t1;
        else{
            t1.val=t1.val+t2.val;
            t1.left=mergeBinaryTree(t1.left,t2.left);
            t1.right=mergeBinaryTree(t1.right,t2.right);
            return t1;
        }
    }
}

转载于:https://www.cnblogs.com/yjxyy/p/10720466.html

相关文章:

  • 03与08组策略区别
  • [Pytorch] pytorch笔记 三
  • 五子棋游戏
  • TMG 2010 部署与入门配置
  • 搭建自己的code-push-server服务器
  • Hyper-V 配置网络 (高级篇)
  • HTTP劫持和DNS劫持
  • 关于语言的争论
  • webservice学习教程(一):理论
  • C中int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t区别
  • 自制jQuery图片切换插件ImgSlider v1.0
  • CORS简介
  • 《Android/OPhone 开发完全讲义》目录
  • mac pycharm快捷键
  • asp.net architecture
  • 网络传输文件的问题
  • C++类的相互关联
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • CSS实用技巧
  • egg(89)--egg之redis的发布和订阅
  • ES6 学习笔记(一)let,const和解构赋值
  • flutter的key在widget list的作用以及必要性
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • HTTP请求重发
  • HTTP--网络协议分层,http历史(二)
  • Joomla 2.x, 3.x useful code cheatsheet
  • js ES6 求数组的交集,并集,还有差集
  • Linux链接文件
  • Lucene解析 - 基本概念
  • MySQL-事务管理(基础)
  • PHP面试之三:MySQL数据库
  • QQ浏览器x5内核的兼容性问题
  • Quartz初级教程
  • 回顾 Swift 多平台移植进度 #2
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 思否第一天
  • 消息队列系列二(IOT中消息队列的应用)
  • 移动端唤起键盘时取消position:fixed定位
  • 【干货分享】dos命令大全
  • ​业务双活的数据切换思路设计(下)
  • #include
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (Java数据结构)ArrayList
  • (初研) Sentence-embedding fine-tune notebook
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (十) 初识 Docker file
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (转)nsfocus-绿盟科技笔试题目
  • .gitignore文件设置了忽略但不生效
  • .NET Core 和 .NET Framework 中的 MEF2
  • //解决validator验证插件多个name相同只验证第一的问题
  • /etc/sudoer文件配置简析
  • [ 网络基础篇 ] MAP 迈普交换机常用命令详解
  • [\u4e00-\u9fa5] //匹配中文字符