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 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.
分析:
给定两个二叉树,合并他们,也就是将节点处的值加和。
遍历求解,先检查当前节点是否存在,如果一个节点不存在,则返回另一个节点,如果两个节点都存在则将val加和,我们可以复用其中的一颗树,处理完当前节点后,递归求解子树。
程序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(t1 == nullptr) return t2;
if(t2 == nullptr) 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合并二叉树 (C++)的更多相关文章
- [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] 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 解题报告
		题目要求 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 二叉树
		题意: 给定两棵树,将两棵树合并成一颗树 输入 Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出 合并的树 3 / \ 4 5 / \ \ 5 4 7 ... 
- Leetcode617.Merge Two Binary Trees合并二叉树
		给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ... 
- 【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
		原题 Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ... 
随机推荐
- runloop是iOS系统上的actor模式
			runloop是iOS系统上的actor模式(单线程派发的) 
- Spark项目之电商用户行为分析大数据平台之(十二)Spark上下文构建及模拟数据生成
			一.模拟生成数据 package com.bw.test; import java.util.ArrayList; import java.util.Arrays; import java.util. ... 
- linux虚拟化课堂总结图2019_4_23
- Nginx端口占用问题
			错误信息:nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use) 主要是端口被占用 ... 
- opencv 显示摄像头数据
			本文章是一个小例子,主要是在ubuntu 系统中利用Opencv 显示摄像头的数据 ,显示到对话框中. 1.建立一个 main.cpp #include<opencv2/core/core.h ... 
- nginx反向代理 强制https请求 + 非root用户起80,443端口
			1. #强制使用https跳转 return 301 https://$server_name$request_uri;rewrite ^(.*)$ https://${server_name}$1 ... 
- 几个PHP读取整个文件的函数readfile()、fpassthru()和file()
			2.7.4 读取整个文件:readfile().fpassthru()和file()除了可以每次读取文件一行外,还可以一次读取整个文件.PHP提供了4种不同的方式来读取整个文件.第一种方式是rea ... 
- 第12章     GPIO输入—按键检测
			第12章 GPIO输入—按键检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ... 
- Linux下端口被占用确认
			有时候关闭软件后,后台进程死掉,导致端口被占用.下面以JBoss端口8083被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln netstat -tln | g ... 
- HDFS--大数据应用的基石
			近些年,由于智能手机的迅速普及推动移动互联网技术的蓬勃发展,全球数据呈现爆发式的增长.2018年5月企鹅号的统计结果:互联网每天新增的数据量达2.5*10^18字节,而全球90%的数据都是在过去的两年 ... 
