当前位置:网站首页>【二叉树-中等】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)
边栏推荐
- The shell specifies the parameter name to pass the parameter
- Shader Graph learns various special effects cases
- 深度学习(五) CNN卷积神经网络
- [Swoole Series 3.5] Process Pool and Process Manager
- 2022年8月1日-8月7日(本周10小时,合计1422小时,剩余8578小时)
- [LeetCode] Find the sum of the numbers from the root node to the leaf node
- Summary of Web Performance Testing Models
- 墨西哥大众VW Mexico常见的几种label
- 数组(一)
- 通关剑指 Offer——剑指 Offer II 012. 左右两边子数组的和相等
猜你喜欢
随机推荐
【内存管理概述 Objective-C语言】
STM32F103驱动HCSR04超声波测距显示
自动化测试中,测试数据与脚本分离以及参数化方法
免费文档翻译软件电脑版软件
【wpf】拖拽的简单实现
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
QT中,QTableWidget 使用示例详细说明
Premint工具,作为普通人我们需要了解哪些内容?
C# rounding MidpointRounding.AwayFromZero
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
RESOURCE_EXHAUSTED: etcdserver: mvcc: database space exceeded
C# winform 单选框
多线程之自定义线程池
unity编辑器扩展界面使用 List
进程管理和任务管理
首次在我们的centos上安装MySQL
OOD论文:Revisit Overconfidence for OOD Detection
Open3D 泊松盘网格采样
In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?
flask增删改查