[leetcode-617-Merge Two Binary Trees]

时间:2022-05-01 10:13:12

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

思路:

类似于先序遍历二叉树,递归操作。

TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2)
{
if(t1==NULL)return t2;
if(t2==NULL)return t1;
t1->val+=t2->val;
t1->left = mergeTrees(t1->left,t2->left);
t1->right = mergeTrees(t1->right,t2->right);
return t1;
}

[leetcode-617-Merge Two Binary Trees]的更多相关文章

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

  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++)

    题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

  4. Leetcode 617 Merge Two Binary Trees 二叉树

    题意: 给定两棵树,将两棵树合并成一颗树 输入 Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出 合并的树 3 / \ 4 5 / \ \ 5 4 7 ...

  5. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

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

  7. 【LeetCode】617. Merge Two Binary Trees 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  8. [LeetCode&Python] Problem 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 ...

  9. 【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 o ...

  10. LeetCode 617. Merge Two Binary Tree (合并两个二叉树)

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

随机推荐

  1. MFC学习随笔(2)

    在MFC中,有时需要多个类之间传递信息,一个共通的头文件是个不错的选择.如果在头文件中直接声明一个变量的话,势必会报出一个错误: error LNK2005: "struct my_glob ...

  2. JS中的专业术语

    本身虽然是学技术出身,但.....此处省略N个字符 1.Namespace 命名空间 允许开发人员在一个独特, 应用相关的名字的名称下捆绑所有功能的容器. 2.Class类 定义对象的特征.它是对象的 ...

  3. 增强型for循环,用于遍历数组元素

    /** * */ package com.cn.u4; /** * @author Administrator *增强型for */ public class ZhengQiangFor { publ ...

  4. 正整数从1到N,输出按照字典序排序的前K个数

    #include <iostream> #include <cassert> using namespace std; ; char a[max_len]; void topK ...

  5. 【CSS】圆角阴影边框CSS

    .someClassName { width:300px; display: inline-block; padding: 5px 10px 6px; text-decoration: none; b ...

  6. hdu5444(模拟)

    题意:建树,给你几个点,要求输出走到各个点的路径(左为E,右为W,树的遍历) 二叉树的模拟题,但是GG了两次. 主要是没注意到直接模拟会爆掉- -,进行下处理就好了 #include <iost ...

  7. MarkDownPad2基本语法

    一.换行和空格   (1)换行   行尾加两个空格   (2)空格     二.标题   在#后跟个空格再写文字,一个#是一级标题,两个#是二级标题,以此类推,支持六级标题.   示例: # 一级标题 ...

  8. 解决ubuntu 14&period;04 &OpenCurlyDoubleQuote;E&colon; 无法获得锁 &sol;var&sol;lib&sol;apt&sol;lists&sol;lock - open &lpar;11&colon; 资源暂时不可用&rpar;”的问题

    http://blog.csdn.net/nicolaskaiqi/article/details/39761757

  9. 使用oracle导出的dmp文件(包含表结构还是表数据&quest;)

    我们都知道oracle提供了一个exp程序,可以导出dmp文件,那么dmp文件中到底包含哪些东西呢? 1:有对象的信息吗?比如对象的权限? 2:有表空间信息吗? 3:有表结构吗? 4:有表的索引和触发 ...

  10. vue系列之项目优化

    webpack中的Code Splitting Code Splitting是什么以及为什么 在以前,为了减少HTTP请求,通常地,我们会把所有的代码都打包成一个单独的JS文件,但是,如果这个文件体积 ...