leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?
/*
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.
*//**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2) {
}
*/
题意:合并两个二叉树。规则是,对应的两个结点如果有重叠就求和(比如更改val),如果不重叠就使用非空的结点的值。(不考虑树释放的问题。)

办法1
* 把t2并到t1,最终返回t1。
* 记录t1,作为最终的返回。进入递归函数。
* 如果t1与t2都不为空,此时需要递归:
* 求和,更改t1->val。
* 左子树递归,并把结果赋值给t1->left。
* 右子树递归,并把结果赋值给t1->right。
* 返回t1。
* 否则,结束递归,返回t1?t1:t2。
* 注意,递归的返回值要利用上,这是更换子树的机会。
强化与提问
* 递归关键点:
* 只考虑两层,root跟(root->left && root->right)。
* 何时递归。
* 何时结束递归。
* 方法不一样时,要留意的点很可能也不一样。在一个方法中容易犯的错,在另一个方法中可能根本就不存在。所以,想办法有时候是解决问题的关键。
* 代码上的细节,有时决定了成败。
#include <stdio.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* merge(struct TreeNode* l, struct TreeNode* r) {
if (l && r) {
l->val += r->val;
l->left = merge(l->left, r->left);
l->right = merge(l->right, r->right);
return l;
}
else {
return l?l:r;
}
}
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2) {
struct TreeNode* ret = t1;
merge(t1, t2);
return ret;
}
void printTree(struct TreeNode* tree) {
if (tree) {
printf("%d, ", tree->val);
printTree(tree->left);
printTree(tree->right);
}
else {
printf("NULL, ");
}
}
int main(int argc, char *argv[])
{
struct TreeNode n1={1,0,0};
struct TreeNode n2={3,0,0};
struct TreeNode n3={2,0,0};
struct TreeNode n4={5,0,0};
n1.left = &n2;
n1.right = &n3;
n2.left = &n4;
struct TreeNode r1={2,0,0};
struct TreeNode r2={1,0,0};
struct TreeNode r3={3,0,0};
struct TreeNode r4={4,0,0};
struct TreeNode r5={7,0,0};
r1.left = &r2;
r1.right = &r3;
r2.right = &r4;
r3.right = &r5;
printf("tree1:\n");
printTree(&n1);
printf("\ntree2:\n");
printTree(&r1);
printf("\nmerged:\n");
struct TreeNode* ret = mergeTrees(&n1, &r1);
printTree(ret);
printf("\n");
return 0;
}
leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?的更多相关文章
- 【算法题目】Leetcode算法题思路:两数相加
在LeetCode上刷了一题比较基础的算法题,一开始也能解出来,不过在解题过程中用了比较多的if判断,看起来代码比较差,经过思考和改进把原来的算法优化了. 题目: 给出两个 非空 的链表用来表示两个非 ...
- LeetCode刷题--21.合并两个有序链表(简单)
题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1 -> 2 -> 4 ,1 -> 3 -> 4 输出:1 ...
- leetcode刷题-88.合并两个有序数组
题目 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 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每日一题]88. 合并两个有序数组
[LeetCode每日一题]88. 合并两个有序数组 问题 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 初始化 n ...
- LeetCode算法题-Merge Two Binary Trees(Java实现)
这是悦乐书的第274次更新,第290篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第142题(顺位题号是617).提供两个二叉树,将其合并为新的二叉树,也可以在其中一个二 ...
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- LeetCode算法题-Reach a Number(Java实现)
这是悦乐书的第310次更新,第331篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第179题(顺位题号是754).你站在无限数字线的0号位置.在目的地有个target.在 ...
- LeetCode算法题-Longest Univalue Path(Java实现)
这是悦乐书的第290次更新,第308篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第158题(顺位题号是687).给定二叉树,找到路径中每个节点具有相同值的最长路径的长度 ...
随机推荐
- 【UML 建模】类图介绍
1.类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 2.类的关系有泛化(Generalization). ...
- java 上传文件-生成文件首页缩略图 生成pdf 抓取图片
方法:1.文件转换成pdf(采用openoffice或者jacob) 2.抓取pdf首页图 第一步:采用jacob: a.下载jacob 注意区分32位,64位,否则不能用 将dll文件放在ja ...
- 基于HTML5及WebGL的工控SCADA模拟飞机飞行
昨天看到一篇文章说是学习如何开飞机的,然后我就想,如果我也可以开飞机那就好玩了,每个人小时候都想做飞行员!中国飞行员太难当了,再说也不轻易让你开飞机!后来我就想如果能用 HT 开飞机那就是真的有趣了, ...
- Android Weekly Notes Issue #281
October 29th, 2017 Android Weekly Issue #281 本期内容不多,包含了小众DI库牙签帮助测试的文章,Kotlin中Delegate的强大之介绍,以及基于Goog ...
- kettle闪退问题
确定Java的配置环境没问题 kettle闪退的时候把spoon.bat里面的配置项改一下 if "%PENTAHO_DI_JAVA_OPTIONS%"=="" ...
- yum常用选项和参数
列举包文件 # yum list #列出资源库中所有可以安装或更新的rpm包 # yum list updates #列出资源库中所有可以更新的rpm包 # yum list installed #列 ...
- 用python做小说网站
html头部 {% extends 'base.html' %} {% load static %} {% block title %}小说首页{% endblock %} {% block cont ...
- [译]ASP.NET Core 2.0 系列文章目录
基础篇 [译]ASP.NET Core 2.0 中间件 [译]ASP.NET Core 2.0 带初始参数的中间件 [译]ASP.NET Core 2.0 依赖注入 [译]ASP.NET Core 2 ...
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
前两天有人私信我,让我爬这个网站,http://bbs.baobeihuijia.com/forum-191-1.html上的失踪儿童信息,准备根据失踪儿童的失踪时的地理位置来更好的寻找失踪儿童,这种 ...
- 玩转 HTML5 下 WebGL 的 3D 模型交并补
建设性的立体几何具有许多实际用途,它用于需要简单几何对象的情况下,或者数学精度很重要的地方,几乎所有的工程 CAD 软件包都使用 CSG(可以用于表示刀具切削,以及零件必须配合在一起的特征).CSG ...