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 方法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 二叉树的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- 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 ...
- 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 ...
- 【LeetCode】617. Merge Two Binary Trees 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【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 ...
- [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 ...
随机推荐
- UVA Bandwidth
题目例如以下: Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and a ...
- 几款用jQuery写的h5小游戏
人人都说前端用来做游戏是一件很困难的事情,遇到这些js的逻辑性问题,是不是有点懵?其实,做完一款游戏之后就会发现,没啥难的地方,差不多都是换汤不换药,作为爱玩游戏的我,也总结收集了几款比较流行的小软件 ...
- 【a903】石子归并
Time Limit: 10 second Memory Limit: 2 MB 问题描述 在一个圆形操场的四周摆放着N堆石子(N<= 100),现要将石子有次序地合并成一堆.规定每次只能选取相 ...
- udacity android 实践笔记: lesson 4 part a
udacity android 实践笔记: lesson 4 part a 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- Hadoop配置文件 分类: A1_HADOOP 2014-08-19 12:48 1157人阅读 评论(1) 收藏
部分内容参考:http://www.linuxqq.net/archives/964.html http://slaytanic.blog.51cto.com/2057708/1100974/ ha ...
- [Angular Unit Testing] Testing Component methods
import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...
- iOS开发之Quzrtz2D 一:认识Quzrtz2D
一:什么是Quzrtz2D 二:Quzrtz2D实例: 三:图形上下文 四:Quzrtz2D在ios开发中的实际价值 1.什么是Quartz2D? 他是一个二维的绘图引擎,同时支持iOS和Mac系统 ...
- skip-slave-start的重要性
原来做复制的主机因为数据丢失需要重新创建复制环境,机器上已经有了主库数天前的备份,于是删除数据目录直接把备份放上去,结果发现复制没有抱错,show slave status一切正常,select co ...
- .netcore consul实现服务注册与发现-集群完整版
原文:.netcore consul实现服务注册与发现-集群完整版 一.Consul的集群介绍 Consul Agent有两种运行模式:Server和Client.这里的Server和Clien ...
- 二维高斯滤波器(gauss filter)的实现
我们以一个二维矩阵表示二元高斯滤波器,显然此二维矩阵的具体形式仅于其形状(shape)有关: def gauss_filter(kernel_shape): 为实现二维高斯滤波器,需要首先定义二元高斯 ...