617. Merge Two Binary Trees(Easy)
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.
/* 合并的树是新建结点
递归的思想,自己写不出来。。看discuss后觉得很简单,真尴尬,其实就是个二叉树的前序遍历,先处理根节点,然后递归处理两个左右子树。
*/ /**
* 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 == NULL && t2 == NULL) return NULL;
TreeNode* root = new TreeNode((t1 != NULL ? t1 -> val: ) + (t2 != NULL ? t2 -> val: ) ); // 初始化根节点,根据t1和t2的存在情况
root -> left = mergeTrees((t1 != NULL ? t1 -> left : NULL), (t2 != NULL ? t2 -> left : NULL));
root -> right = mergeTrees((t1 != NULL ? t1 -> right : NULL), (t2 != NULL ? t2 -> right : NULL)); return root;
}
};
617. Merge Two Binary Trees(Easy)的更多相关文章
- 【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 ...
- 17.Merge Two Binary Trees(合并两个二叉树)
Level: Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...
- 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 ...
- [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&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 ...
- [Algorithm] 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 ...
随机推荐
- mssql sql语句过滤百分号的方法分享
转自:http://www.maomao365.com/?p=6743 摘要: 下文讲述sql脚本中过滤百分号的方法: 实验环境:sql server 2008 R2 百分号:在sql脚本编写中“百 ...
- 解决Eclipse点击运行后控制台不能自动弹出的问题
解决方案: 选择Window-->Preferences-->Run.Debug-->Console 勾选"Show when program writest to sta ...
- 【转载】failed to initialize nvml driver/library version mismatch ubuntu
英伟达驱动版本是384.130 显示的NVRM version: NVIDIA UNIX x86_64 Kernel Module是:384.130. 若是旧的版本就会出现如下问题. 这个问题出现的原 ...
- February 20th, 2018 Week 8th Tuesday
Receive without conceit, release without struggle. 接受时,不狂妄:放手时,不犹豫. How to understand this quote? Do ...
- 【PS技巧】如何校正倾斜的图片
1.打开PS,直接拖拽图片. 2.点击[滤镜==>扭曲==>镜头校正],出现校正对话框. 3.点击拉直工具,从右向左滑一条直线. 参考文档: 在Photoshop中如何校正倾斜的图片?
- TCP长连接保持连接状态
对于TCP长连接保活是十分必要的,原因如下: 1.系统多在OA网和外网间有防火墙隔离,很多防火墙对一段时间内没有报文活动的socket会自动关闭. 2.对于非正常断开的连接系统并不能侦测到,比如防火墙 ...
- 【NOI2018模拟5】三角剖分Bsh
[NOI2018模拟5]三角剖分Bsh Description 给定一个正 n 边形及其三角剖分,共 2n - 3 条边 (n条多边形的边和n-3 条对角线),每条边的长度为 1. 共 q 次询问,每 ...
- SQL IN 操作符
IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...
- 5.05-requests_cookies2
import requests # 请求数据url member_url = 'https://www.yaozh.com/member/' headers = { 'User-Agent': 'Mo ...
- 提升SQLite数据插入效率低、速度慢的方法
前言 SQLite数据库由于其简单.灵活.轻量.开源,已经被越来越多的被应用到中小型应用中.甚至有人说,SQLite完全可以用来取代c语言中的文件读写操作.因此我最近编写有关遥感数据处理的程序的时候, ...