当前位置:网站首页>【二叉树-中等】1379. 找出克隆二叉树中的相同节点
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
2022-08-10 01:52:00 【菜菜2022】
【题目】
【代码】
【方法1】这个是大多数人的解法,其实是不对的,虽然结果正确
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
ans=None
def dfs(root):
nonlocal ans
if not root:
return
if root.val==target.val and ans==None:
ans=root
dfs(root.left)
dfs(root.right)
dfs(cloned)
return ans
【方法2】正确解法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
if not original:
return None
if original==target: return cloned
left=self.getTargetCopy(original.left,cloned.left,target)
if left:return left
return self.getTargetCopy(original.right,cloned.right,target)
边栏推荐
- unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
- LeetCode 每日一题——1413. 逐步求和得到正数的最小值
- SonarQube升级记录:7.8->7.9->8.9
- 实操|风控模型中常用的这三种预测方法与多分类场景的实现
- Screen 拆分屏幕
- 781. 森林中的兔子
- [语法糖] 关于类别字符串到类别数字id的映射
- openpose脚部标注问题梳理
- 宝塔服务器PHP+mysql网页URL跳转问题
- RESOURCE_EXHAUSTED: etcdserver: mvcc: database space exceeded
猜你喜欢

实操|风控模型中常用的这三种预测方法与多分类场景的实现

FusionCompute产品介绍

Shader Graph learns various special effects cases

手把手教你编写性能测试用例

Unity image is blurry after using long image

sql实战积累

组件的使用

Experimental support for decorators may change in future releases.Set the "experimentalDecorators" option in "tsconfig" or "jsconfig" to remove this warning

数据库治理利器:动态读写分离

Janus实际生产案例
随机推荐
翻译软件免费版下载-免费版翻译软件下载
浏览器中的history详解
OpenCV图像处理学习三,Mat对象构造函数与常用方法
基于C51的中断控制
小程序开发的报价为什么有差别?需要多少钱?
[网鼎杯 2020 青龙组]AreUSerialz
[Turn] Typora_Markdown_ picture title (caption)
墨西哥大众VW Mexico常见的几种label
ImportError: Unable to import required dependencies: numpy
SQL注入的order by ,limit与宽字节注入
万字总结:分布式系统的38个知识点
idea 删除文件空行
2022杭电多校联赛第七场 题解
C# winform 单选框
Not, even the volume of the king to write code in the company are copying and pasting it reasonable?
OpenCV图像处理学习一,加载显示修改保存图像相关函数
具有多孔光纤的偏振分束器
【Swoole系列3.5】进程池与进程管理器
C# 四舍五入 MidpointRounding.AwayFromZero
【wpf】自定义事件总结(Action, EventHandler)