当前位置:网站首页>LeetCode之合并二叉树
LeetCode之合并二叉树
2022-06-30 16:50:00 【little亮_】
题目
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
示例 1:
输入:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
输出:[3,4,5,5,4,null,7]
示例 2:输入:root1 = [1], root2 = [1,2]
输出:[2,2]
提示:
两棵树中的节点数目在范围 [0, 2000] 内
-104 <= Node.val <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/merge-two-binary-trees
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
# 将二叉树的对应位置进行合并
'''
三种情况:
1.左子树和右子树都有值,则相加即可
2.左(右)子树有值,右(左)子树无值,则只添加左子树
3.左子树无值,右子树无值,则不添加
遍历到任何一颗节点都是这样的,因此可以使用递归,当遍历完当前节点后,可以
再分别递归遍历当前节点的左子树和右子树
'''源码
# 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:
# 只要有一颗子树遍历完结,则另一颗子树剩下的全部返回
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
通过截图

同步更新于个人博客系统:LeetCode之合并二叉树
边栏推荐
- 现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
- Sign up for Huawei cloud proposition in the "Internet +" competition, and you can take many gifts!
- Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases
- Redis (I) - data type
- LRN local response normalization
- Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
- Flutter custom component
- Compile and generate busybox file system
- VScode 状态条 StatusBar
- MySQL advanced - Architecture
猜你喜欢

vue3 响应式数据库—— reactive

Design of online shopping mall based on SSH
![leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]](/img/28/78e2961877776ca3dfcba5ee7e35d2.png)
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]

基于SSH的网上商城设计

Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard
![The company was jailed for nonstandard bug during the test ~ [cartoon version]](/img/cd/42ab3fc0000fa7dfe2ac89de3486e4.jpg)
The company was jailed for nonstandard bug during the test ~ [cartoon version]

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集

Daily interview 1 question - basic interview question of blue team - emergency response (1) basic idea process of emergency response +windows intrusion screening idea

Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)

TFTP download kernel, NFS mount file system
随机推荐
助力极致体验,火山引擎边缘计算最佳实践
Thinking on large file processing (upload, download)
Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
Switching routing (VLAN) experiment
Php8.0 environment detailed installation tutorial
Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides
阿里云ECS导入本地,解决部署的问题
Spin lock exploration
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
The secondary menu of the magic article system v5.4.0 supports the optimization of form display
Type ~ storage ~ variable in C #
力扣解法汇总1175-质数排列
The company was jailed for nonstandard bug during the test ~ [cartoon version]
IEEE TBD SCI impact factor increased to 4.271, ranking Q1!
Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
News management system based on SSM
Shortcut keys for the rainbow brackets plug-in
One script of unity actual combat realizes radar chart
VScode 状态条 StatusBar
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
