[leetcode] 653. Two Sum IV - Input is a BST @ python

LeetCode --- 653. Two Sum IV - Input is a BST 解题报告 Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: I. 阅读详情

原题

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:

Input:
5
/
3 6
/ \
2 4 7

Target = 9

Output: True

Example 2:

Input:
5
/
3 6
/ \
2 4 7

Target = 28

Output: False

解法1

中序遍历, 将节点的值放入列表, 然后遍历列表, 查找是否存在两个数的和为k
Time: O(2*n)
Space: O(n)

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findTarget(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: bool
        """
        l = []
        def inOrder(root):
            if not root:
                return
            inOrder(root.left)
            l.append(root.val)
            inOrder(root.right)
        
        inOrder(root)
        for n in l:
            if k-n in l and k-n!= n:
                return True
        return False

解法2

BFS. 将每一层的节点放入队列, 检查已见过的节点中是否有一个节点, 使得与当前节点的和等于k.

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findTarget(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: bool
        """
        if not root: return False
        q = [root]
        seen = set()
        while q:
            for node in q:
                if k-node.val in seen:
                    return True
                else:
                    seen.add(node.val)
            q = [kid for node in q for kid in (node.left, node.right) if kid]
        return False
深入排查127.0.0.1拒绝连接:从端口占用到Tomcat服务启动的完整解决方案 本文针对开发者访问127.0.0.1时遭遇“拒绝连接”的常见问题,提供了一套完整的排查与解决方案。文章从端口占用检查入手,详细讲解了使用netstat命令识别进程,并深入分析了服务监听地址、IPv6配置、防火墙以及Tomcat服务安装与启动等核心环节。通过实战演练,指导读者系统性地解决从网络层到应用层的各类连接问题,最终确保本地服务成功运行并可通过本地回环地址正常访问。 阅读详情

相关推荐

《全连接到卷积神经网络:图像识别的演变历程》

这篇文章会从传统全连接层面临的困境说起,揭示卷积神经网络(CNN)成为图像识别核心动力的原因,探究其背后蕴含的设计思想与技术创新。但是回顾历史,CNN的出现提醒我们:技术的突破往往是由对自然规律的模仿和重新构建得来的。也许,当机器真的学会“像人一样看世界”的时候,它们的“眼睛”早就超过生物视觉的限制了。例如,用5×5的核处理224×224的输入时,只要25个参数(而不是全连接那种百万级的)。* 局部性:网络的底层只需留意局部的地方(像边缘、纹理这样的地方),不用管那些全局像素之间复杂的关系。

2401_82434295的博客 674

leetcode 653. Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \

淡然坊 2079

ADRV9009学习资料保存:基于XC7Z100+ADRV9009的双收双发无线电射频板卡

ADRV9009学习资料保存:基于XC7Z100+ADRV9009的双收双发无线电射频板卡 一、板卡概述 基于XC7Z100+ADRV9009的双收双发无线电射频板卡是基于XilinxZYNQFPGA和ADI的无线收发芯片ADRV9009开发的专用功能板卡,用于5G小基站,无线图传,数据收发...

9375

【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

【LeetCode】653. Two Sum IV - Input is a BST 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ 题目描述: Given a Binary Search Tree and a target numbe

负雪明烛 2630

【YOLO11改进 - C3k2融合】C3k2融合AKConv(可改变核卷积)二次创新:任意数量的参数和任意采样形状的即插即用的卷积

【YOLO11改进 - C3k2融合】C3k2融合AKConv(可改变核卷积)二次创新:任意数量的参数和任意采样形状的即插即用的卷积

专注于图像领域,主要研究内容包括计算机视觉和深度学习,特别是在图像分类、目标检测和图像生成等方面有深入的研究和实践经验。 407

【Halcon-2D测量】add_metrology_object_rectangle2_measure 函数功能(用于向2D计量模型中添加旋转矩形(rectangle2类型)计量对象)

HALCON算子add_metrology_object_rectangle2_measure解析摘要 该算子是HALCON 2D计量模块的核心算子,用于添加旋转矩形计量对象并配置测量参数。主要功能包括:定义旋转矩形几何轮廓(中心坐标、主轴角度、长短半长);生成垂直于矩形边界的测量区域;设置边缘提取预处理参数(高斯平滑、振幅阈值)。关键注意事项:MeasureLength1必须小于矩形长短半长;角度参数Phi需使用弧度单位;多线程需同步模型句柄。该算子适用于方形螺母、PCB板边框等矩形零件的精密测量,需与a

weixin_45590420的博客 1225

LeetCode Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \

渣渣 1240

MATLAB基础应用精讲-【数模应用】游程检验(附MATLAB、R语言和python代码实现)

游程检验是一种非参数性统计假设的检验方法,它是游程总数检验和最大游程检验的总称,用于两个独立样本的比较和观测结果随机性的检验测试。​游程检验亦称“连贯检验”,是根据样本标志表现排列所形成的游程的多少进行判断的检验方法。[2]例1:设某样本n=12人的标志表现为男、女,有以下三种排列。(i) 男\男,女\女\女,男,女\女,男\男\男\男;(ii) 男\男\男\男\男\男\男,女\女\女\女\女;(iii) 男,女,男,女,男,女,男,女,男,女,男\男;连续出现男或女的区段称为游程。

qq_36130719的博客 609

Leetcode 653. Two Sum IV - Input is a BST

Leetcode 653. Two Sum IV - Input is a BST —————————————– Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the g

两鬓已不能斑白的专栏 2074

《网络安全自学教程》- Windows基线检查加固

Windows安全配置,基线检查标准,基线加固方式

wangyuxiang946的博客 1万+

【人工智能】AI驱动值班巡检Agent(Hermes)

传统运维值班巡检场景中,存在四大核心痛点:本项目基于 Agent+Skills+浏览器自动化 架构,构建了具备自我进化能力的AI值班员,核心逻辑流如下:

奶爸的编程之路,也就一周冷个三天 178

python--leetcode653. Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \

wenqiwenqi123的博客 959

Openclaw错误排查及解决方案之:Something went wrong while processing your request. Please try again.

Openclaw错误排查及解决方案之:Something went wrong while processing your request. Please try again.

多媒体编程、网络编程、系统编程、网络安全编程、驱动编程 3790

[leetcode]653. Two Sum IV - Input is a BST(Python)

[leetcode]653. Two Sum IV - Input is a BST题目描述题目理解解题思路BFSMyMethodTime 题目描述 Category:Easy Tree Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such t...

La_Ragazza的博客 431

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode之653. Two Sum IV - Input is a BST

* 这题属于Two Sum题,将BST中根遍历存到数组当中,转换为数组的Two Sum问题即可 * C # 代码 public bool FindTarget(TreeNode root, int k) { List<int> lst = new List<int>(); BST2Lst(root,r...

如切如磋,如琢如磨 311

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LeetCode 653. Two Sum IV - Input is a BST--Python解法

LeetCode 653. Two Sum IV - Input is a BST–Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++和Python的解法都有。 题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tr...

个人博客 460

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode 653 Two Sum IV - Input is a BST 解题报告

题目要求 Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. 题目分析及思路 给定一个二叉搜索树和一个目标值,若该树中存在两个元素的和等于目标值则返...

weixin_30617561的博客 85

[leetcode] 653. Two Sum IV - Input is a BST

Description Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 ...

Learning from the mistakes 163

653. Two Sum IV - Input is a BST

1. Question: 653.Two Sum IV - Input is a BST url:https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ Given a Binary Search Tree and a target number, return true if there exis...

weixin_30802273的博客 89

leetcode 653 Two Sum IV - Input is a BST

Javaclass Solution { public boolean findTarget(TreeNode root, int k) { HashSet set=new HashSet<>(); return fds(root,set,k); } public boolean fds(TreeNode root,HashSet

MRxjh的博客 317

LeetCode 653. Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1: Input: 5 / \ 3 6 / \ \ 2

Dev-L的博客 395

LeetCode---653. Two Sum IV - Input is a BST

题目 给出一个二叉搜索树和一个目标值,找到两个节点值得和为给定的目标值。 Python题解 class Solution(object): def findTarget(self, root, k): """ :type root: TreeNode :type k: int :rtype: bool ...

coderate 235

[LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers ...

weixin_30329623的博客 108

653. Two Sum IV - Input is a BST(python+cpp)

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target Example 1: Input: 5 / \ 3 6 / \...

小湉湉的博客 216

Leetcode 653. Two Sum IV - Input is a BST 两数相加4 解题报告

恩,还是给一个数,问有给定的二叉搜索树,有没有两个数相加的和等于这个数恩,我是最直接的做法,开空间记录出现过的数字。。其实,这道题的思想,我猜应该是要求一个空间为O(1)的解法。。但是觉得这么做太复杂,就算了吧Given a Binary Search Tree and a target number, return true if there exist two elements in the B

MebiuW的专栏 3540
上一篇: [leetcode] 637. Average of Levels in Binary Tree @ python
下一篇: [leetcode] 507. Perfect Number @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值