当前位置:网站首页>Merged binary tree of leetcode
Merged binary tree of leetcode
2022-06-30 18:30:00 【Little light_】
subject
Here are two binary trees : root1 and root2 .
Imagine , When you cover one tree over the other , Some nodes on the two trees will overlap ( And others don't ). You need to merge these two trees into a new binary tree . The rule of consolidation is : If two nodes overlap , Then add the values of the two nodes as the new values of the merged nodes ; otherwise , Not for null The node of will be the node of the new binary tree directly .
Returns the merged binary tree .
Be careful : The merge process must start at the root 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]
Tips :
The number of nodes in the two trees is in the range [0, 2000] Inside
-104 <= Node.val <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/merge-two-binary-trees
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
# Merge the corresponding positions of the binary tree
'''
Three situations :
1. Both the left subtree and the right subtree have values , Then add them
2. Left ( Right ) Subtree has value , Right ( Left ) Subtree has no value , Only the left subtree is added
3. The left subtree has no value , Right subtree has no value , Do not add
Traversing to any node is like this , So you can use recursion , After traversing the current node , Sure
Then recursively traverse the left and right subtrees of the current node respectively
'''Source code
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode:
# As long as a subtree traversal is completed , Then all the rest of the other subtree will return
if not root1: return root2
if not root2: return root1
root = TreeNode(root1.val + root2.val)
root.left = self.mergeTrees(root1.left, root2.left)
root.right = self.mergeTrees(root1.right, root2.right)
return root
Through screenshots

Synchronous update in personal blog system :LeetCode Merge binary tree
边栏推荐
- Redis - persistent RDB and persistent AOF
- Sign up for Huawei cloud proposition in the "Internet +" competition, and you can take many gifts!
- 这里数据过滤支持啥样的sql语句
- Redis (II) -- persistence
- Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
- Php8.0 environment detailed installation tutorial
- MSF后渗透总结
- 分布式场景下,你知道有几种生成唯一ID的方式嘛?
- Daily interview 1 question - how to prevent CDN protection from being bypassed
- Rhai - Rust 的嵌入式脚本引擎
猜你喜欢

depends工具查看exe和dll依赖关系

基于SSM的新闻管理系统

Do you write API documents or code first?

Deep understanding of JVM (II) - memory structure (II)

Only black-and-white box test is required for test opening post? No, but also learn performance test

What will be the game changes brought about by the meta universe?

Daily interview 1 question - how to prevent CDN protection from being bypassed

漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)

Redis (I) - data type

Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
随机推荐
Advanced embedded application of uni app [day14]
LeetCode之合并二叉树
Helping the ultimate experience, best practice of volcano engine edge computing
力扣解法汇总1175-质数排列
Redis (VII) - sentry
Post office - post office issues (dynamic planning)
Redis (IX) - enterprise level solution (II)
Deep understanding of JVM (IV) - garbage collection (I)
C language structure
分布式场景下,你知道有几种生成唯一ID的方式嘛?
Multipass中文文档-设置图形界面
Optimize with netcorebeauty Net core independent deployment directory structure
TCP session hijacking based on hunt1.5
Post MSF infiltration summary
剑指 Offer 16. 数值的整数次方
后渗透之文件系统+上传下载文件
iCloud照片无法上传或同步怎么办?
Only black-and-white box test is required for test opening post? No, but also learn performance test
Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
Unity实战之一个脚本实现雷达图
