题意:

给定两棵树,将两棵树合并成一颗树

输入

	Tree 1                     Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
输出
合并的树
3
/ \
4 5
/ \ \
5 4 7 方法1不需要提供额外内存,但是时间居然很长
 /**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
if t1 == nil && t2 == nil{
return nil
}
if t1 != nil && t2 == nil{
return t1
}
if t1 == nil && t2 != nil{
return t2
}
if t1 != nil && t2 != nil{
t1.Val += t2.Val
t1.Left = mergeTrees(t1.Left, t2.Left);
t1.Right = mergeTrees(t1.Right, t2.Right);
}
return t1;
}

方法2需要提供额外内存,但是时间居然比方法1少,简直不可思议

 /**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func newTreeNode() *TreeNode{
return &TreeNode{
}
} func dfs(m **TreeNode, t *TreeNode){ if t == nil {
return ;
} if (*m) == nil{
(*m) = newTreeNode()
} (*m).Val += t.Val
dfs(&((*m).Left), t.Left)
dfs(&((*m).Right), t.Right) } func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
if t1 == nil && t2 == nil {
return nil
} m := newTreeNode() dfs(&m, t1);
dfs(&m, t2); return m;
}

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合并二叉树 (C++)

    题目: 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 解题报告

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

  4. 【Leetcode_easy】617. Merge Two Binary Trees

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

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

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

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

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

  8. 【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 ...

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

随机推荐

  1. ios越狱开发

    theos/Logos常用命令 %hook 用的最多,意思是钩住一个类. %hook SpringBoard %end %new (v@:) 新建方法 v是返回值@代表参数名 %new(v@:@i) ...

  2. [Yarn] A JavaScript Package Manager

    Yarn is a new JavaScript package manager that aims to be speedy, deterministic, and secure. See how ...

  3. 数据库中暂时表,表变量和CTE使用优势极其差别

    1 在写SQL时常常会用到暂时表,表变量和CTE,这三者在使用时各有优势: 1. 暂时表:分为局部暂时表和全局暂时表. 1.1局部暂时表,创建时以#开头,在系统数据库tempdb中存储. 在当前的链接 ...

  4. HDU 1287 破译密码 异或运算

    http://acm.hdu.edu.cn/showproblem.php?pid=1287 题目: 有个叫"猪头帮"的国家,采用一种简单的文法加密,他们所用的语言里面只有大写字母 ...

  5. ios开发网络学习AFN框架的使用一:get和post请求

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  6. nginx源代码分析--ngx_http_optimize_servers()函数

    这个函数做了连部分工作:1)以port为入口点 将实用的信息存放到hash表内 2)调用ngx_http_init_listening()函数 对port进行监听 1. 在ngx_http_core_ ...

  7. Android Studio Gradle:Resolvedependencies':app:_debugCompile' 问题解决纪录

    问题描述: 第一次使用AndroidStudio打开已经存在的AndroidStudio项目,卡在Gradle:Resolvedependencies':app_debugCompile'步骤,即使进 ...

  8. 访问Ext.ComponentMgr中的组件对象

    Ext.Component是所有Ext组件的基类,所有组件被注册在布局管理器中Ext.ComponentManager, 这样就可以通过Ext.getCmp随时被引用,每种组件都有特定的类型,是Ext ...

  9. [tmux] Customize tmux with tmux.conf

    You can modify tmux's behavior with your tmux configuration file, .tmux.conf. You can use your tmux ...

  10. 【hdu 2177】取(2堆)石子游戏

    Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...