当前位置:网站首页>【二叉树-中等】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)
边栏推荐
- OOD论文:Revisit Overconfidence for OOD Detection
- [QNX Hypervisor 2.2用户手册]10.14 smmu
- C# 单例模式
- 中级xss绕过【xss Game】
- Experimental support for decorators may change in future releases.Set the "experimentalDecorators" option in "tsconfig" or "jsconfig" to remove this warning
- 程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
- 用于X射线光学器件的哈特曼波前传感器
- Under pressure, there must be cowards
- [LeetCode] Find the sum of the numbers from the root node to the leaf node
- 2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
猜你喜欢
OpenCV图像处理学习三,Mat对象构造函数与常用方法
mysql -sql编程
c# 解决CS8602告警 解引用可能出现空引用
FusionCompute产品介绍
STM32F103驱动HCSR04超声波测距显示
In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?
Fusion Compute网络虚拟化
type-C 边充电边听歌(OTG) PD芯片方案,LDR6028 PD充电加OTG方案
不是吧,连公司里的卷王写代码都复制粘贴,这合理?
781. 森林中的兔子
随机推荐
C# 单例模式
Maya制作赛博朋克机器人模型
HCIP——综合交换实验
常用正则备查
volatile 关键字(修饰符 volatile 告诉编译器,变量的值可能以程序未明确指定的方式被改变)
深度学习(五) CNN卷积神经网络
2022年8月1日-8月7日(本周10小时,合计1422小时,剩余8578小时)
unity编辑器扩展界面使用 List
[Swoole Series 3.5] Process Pool and Process Manager
idea 删除文件空行
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
[论文阅读] Diverse Image-to-Image Translation via Disentangled Representations
【web渗透】SSRF漏洞超详细讲解
Open3D 泊松盘网格采样
ImportError: Unable to import required dependencies: numpy
Unity image使用长图后 图片很糊
gbase 8a数据库如何查看数据或数据文件是否正常?
Unity碰撞和触发
中英文互译在线翻译-在线翻译软件
如何让数据库中的数据同步