题意:

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

输入

	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. mysql的入门基础操作

    1.数据库的简单介绍 1.1 什么是数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle  是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IB ...

  2. [Preact] Integrate react-router with Preact

    React-router is the community favourite routing solution - it can handle all of your complex routing ...

  3. XML Parser Errors See Details for more Information XML Parser Error on line 1: Document root ele

    1.错误描写叙述 XML Parser Errors See Details for more Information XML Parser Error on line 1: Document roo ...

  4. stm32的复用与映射

    摘自:https://blog.csdn.net/lincheng15/article/details/51789093 摘自:http://www.51hei.com/bbs/dpj-36242-1 ...

  5. 关于Topsort

    Long time no see. 拓扑排序 英文名称:Topological-sort 别称:toposort or  topsort 拓扑排序是干什么的呢 对一个有向无环图(Directed Ac ...

  6. bootstrap+fileinput插件实现可预览上传照片功能

    实际项目中运用: 功能:实现上传图片,更改上传图片,移除图片的功能 <!DOCTYPE html> <html> <head> <meta charset=& ...

  7. 小米笔记本(13.3 I7) ubuntu14.04下网卡驱动安装

    ubuntu 内核升级到4.6.4(更高版本可能造成系统无法启动) kernel debian包下载地址 http://kernel.ubuntu.com/~kernel-ppa/mainline/v ...

  8. ps树叶的雕刻

    1.学习了图层建立,通道复制,通道载如.图层复制粘贴透明,色阶改动(ctrl+L),蒙板建立(必须不在背景上),蒙板刻图.蒙板画画(白色,黑色),蒙板填充(ctrl+backspace),(atrl+ ...

  9. 事件处理之一:两种方式:监听器与回调 分类: H1_ANDROID 2013-10-31 10:26 3250人阅读 评论(0) 收藏

    Android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...

  10. C# 二分法查找和排序

    using System;using System.Collections.Generic;using System.Text; namespace AAA{    public class Dich ...