当前位置:网站首页>Algorithem_ Merge Two Binary Trees
Algorithem_ Merge Two Binary Trees
2022-04-21 12:50:00 【Mokong 9081】
You are given two binary trees root1 and root2.
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 the two trees 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 the new tree.
<!--more-->
Return the merged tree.
Note: The merging process must start from the root nodes of both trees.
Example 1:
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output: [3,4,5,5,4,null,7]
Example 2:
Input: root1 = [1], root2 = [1,2]
Output: [2,2]
solution
Using recursion , You can see from the above example , The basic calculation is the addition of two values , If I have a theta null, And 0 Add up .
Code logic :
Declare a new TreeNode,TreeNode Of val = root1.val + root2.val,TreeNode Of left= recursive (root1.left, roo2.left),right= recursive (root1.right, root2.right)
The code is as follows :
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func mergeTrees(_ root1: TreeNode?, _ root2: TreeNode?) -> TreeNode? {
if root1 == nil && root2 == nil {
return nil
}
let valValue = (root1 == nil ? 0 : root1!.val) + (root2 == nil ? 0 : root2!.val)
let newNode = TreeNode(valValue)
newNode.left = mergeTrees(root1?.left, root2?.left)
newNode.right = mergeTrees(root1?.right, root2?.right)
return newNode
}
}
版权声明
本文为[Mokong 9081]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211247475837.html
边栏推荐
- How to use mind map and flowchart for free (draw.io vs code integration)
- Operation of simulated examination platform of test question bank for operation certificate of safety management personnel of hazardous chemical business units in 2022
- 最长上升子序列(二)(贪心+二分)
- 实例:用C#.NET手把手教你做微信公众号开发(7)--普通消息处理之位置消息
- Binary tree traversal series 02 Morris traversal
- AES自动生成base64密钥加密解密
- 2022年初级会计职称考试经济法基础练习题及答案
- Pytroch deep learning run cifar10 data set
- Validation数据校验注解
- Go language reflection
猜你喜欢

The 2022 language and intelligent technology competition was upgraded to launch four cutting-edge tasks of NLP

How nodejs converts buffer data to string

二叉树遍历系列02-Morris遍历

What are the problems in the digital transformation of manufacturing industry

框架的灵魂------反射
2020BATJZ大厂Android高级工程师面试题-选择题合集(附答案解析)

二叉树遍历系列01-递归遍历与递归序

2022年监理工程师合同管理练习题及答案

【论文学习】YOLO v2

IPEmotion采集J1939协议信号
随机推荐
只出现一次的数字 II(哈希、位操作、逻辑电路、有限状态自动机)
2022年一级注册建筑师考试建筑物理与设备复习题及答案
AES自动生成base64密钥加密解密
CPT 102_LEC 10
2022年监理工程师考试质量、投资、进度控制练习题及答案
Algorithem_Merge Two Binary Trees
4 years of Android development 13K, completed this 1307 page Android interview full set of real problem analysis, job hopping and salary increase 15K
Revit 二次开发入门教程--用HelloRevit进行程序调试(第四期)
Pytest multi file execution sequence control
框架的灵魂------反射
业内视频超分辨率新标杆,快手&大连理工研究登上CVPR 2022
字段行相同则合并在另外一个字段sql 语句?
字符串计数(转换为进制)
利用Cisco配置VRRP(虚拟路由器冗余协议)
Master slave replication -- 03 -- synchronization data consistency
[linked list] 148 Sort linked list
Operation of simulated examination platform of test question bank for operation certificate of safety management personnel of hazardous chemical business units in 2022
Revit二次开发之创建插件面板(第十五期)
How to use mind map and flowchart for free (draw.io vs code integration)
Machine learning - sklearn-12 (regression family - upper - multiple linear regression, ridge regression, Lasso) (solve multiple collinearity)