leetcode 653. Two Sum IV - Input is a BST(两个数之和,输入是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 given target. Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: I. 阅读详情

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

Example 1:
在这里插入图片描述
Constraints:

The number of nodes in the tree is in the range [1, 104].
-104 <= Node.val <= 104
root is guaranteed to be a valid binary search tree.
-105 <= k <= 105

给出BST,问其中是否存在两个节点值的和=k

思路:
涉及二叉数的遍历,Two sum。
二叉树的遍历随便选一个,这里选前序遍历,Two sum就用一个HashSet保存已经遍历过的数字,如果后面set里面保存的有k-root.val,就说明存在。

    public boolean findTarget(TreeNode root, int k) {
        HashSet<Integer> set = new HashSet<>();
        
        return preTraversal(root, set, k);
        
    }
    
    boolean preTraversal(TreeNode root, HashSet<Integer> set, int k) {
        if(root == null) return false;
        if(set.contains(k - root.val)) return true;
        set.add(root.val);
        if(preTraversal(root.left, set, k)) return true;
        if(preTraversal(root.right, set, k)) return true;
        return false;
    }
HMMER实战:从零部署到序列分析 本文详细介绍了HMMER工具的安装与使用,从源码编译到环境配置,再到核心工具hmmbuild、hmmsearch等的实战应用。通过球蛋白序列分析案例,展示了HMMER在生物信息学中的强大功能,帮助读者快速掌握序列分析技巧。 阅读详情

相关推荐

433MHz无线通信避坑指南:如何解决信号干扰和误码问题

本文深入探讨433MHz无线通信中的信号干扰和误码问题,提供从射频前端设计到协议优化的全链路解决方案。通过天线匹配、电源噪声抑制、曼彻斯特编码改进及实时频点切换等技术,显著提升通信稳定性和距离。适合智能家居和工业无线控制系统的开发者参考。

weixin_29288313的博客 438

LeetCode 问题汇总之递归算法

参考文章:http://fisherlei.blogspot.com 最近在刷LeetCode,开始刷题--面试--刷题。。。 下面将遇到的可以用递归求解的问题归纳于此 1. Combination Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

coder 进阶的专栏 1万+

基于大模型的癫痫手术预测与决策系统技术方案文档

基于大模型的癫痫手术预测与决策系统技术方案文档

嵌入式毕设/课设资料清单(速查手册·文档模板·题库,持续更新)→ 见最新文章 984

Two Sum IV - Input is a BST

知乎三面的一道题 653. Two Sum IV - Input is a BST  方法一:hash 使用哈希表来保存BST中节点的值。每当我们将新节点的值插入哈希表时,我们都会检查哈希表是否包含k-node.val class Solution { public: bool findTarget(TreeNode* root, int k) { unord...

behboyhiex的博客 264

【低轨】Starlink星链-Ku波段下行信号信号结构解析

Starlink播发位于10.7~12.7GHz之间的下行信号,该信号的设计意图是用于宽带网络通信,但是,文章通过对该信号信号体制的详细分析,以及解调出所调制的完整码序列,为利用Starlink信号进行定位解算提供了必要条件。(注,码序列对应的是卫星版本0.9,1.0和1.5)

iseeinvisible的博客 6335

工业自动化实战:用LabVIEW的DSC模块实现MODBUS-TCP设备控制

本文详细介绍了如何利用LabVIEW的DSC模块实现工业自动化中的MODBUS-TCP设备控制。通过环境准备、核心功能解析、状态机实现及典型工业场景实战,帮助工程师快速掌握稳定可靠的通讯技术,提升工业自动化系统的运行效率。

salt9的博客 1067

LeetCode 653. Two Sum IV - Input is a BST - 二叉搜索树(Binary Search Tree)系列题12

题目给定一个二叉搜索树和一个目标数,问是否存在两个节点它们的数值和等于目标数。很容易想到跟数组的Two Sum题解法差不多。一种是利用一个hashmap;另一种是中序遍历BST得到一个有序数组,然后使用Two Pointers法。这也是这题被标注为easy的原因。其实这题还有一种解法就是不需要转换成数组就可以用Two Pointers法。

hgq522的博客 287

MobaXterm简介和使用(服务器连接工具)

MobaXterm 是一个功能强大的远程计算工具,集成了许多常用的远程计算功能,包括 SSH、X11、RDP、VNC、FTP、SFTP 等。

清平乐的技术专栏 2031

LeetCode知识点总结 - 653

LeetCode 653. Two Sum IV - Input is a BST 考点 难度 Hash Table Easy 题目 Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target. 思路 定义一个新function

m0_59773145的博客 180

程序员应用AI大模型创业实现财务自由的100种赚钱方案原理与实战指南

成功的 AI 创业者都遵循着 “技术 + 需求 + 变现” 的铁三角法则。记住:不是要做 “全能大模型”,而是成为某个垂直领域的 “AI 解决方案专家”。现在就开始行动:用周末 2 小时完成第一个 MVP,下周启动种子用户测试,三个月内找到可复制的盈利模式。当你实现第一个 10 万用户时,会发现财务自由的密码藏在持续迭代的每个 0.1 版本更新中。延伸思考:你的技术背景适合切入哪个细分领域?

AI天才研究院 1393

学Simulink--基础微电网场景实例:基于Simulink的微电网并网/孤岛无缝切换控制仿真

本文完成了基于 Simulink 的微电网并网/孤岛无缝切换控制仿真,实现了:✅ 构建含双模式逆变器的微电网模型✅ 实现预同步 + Stateflow 逻辑的智能切换✅ 验证计划与非计划切换的性能边界✅ 证明“无缝”在工程上是可实现的。

amy_mhd的博客 114

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

本题是二叉搜索树的经典问题,主要考察对二叉搜索树性质的理解和应用。通过使用中序遍历 + 双指针或哈希表,我们可以高效地在二叉搜索树中查找两个节点值之和等于目标值。中序遍历 + 双指针的核心思想是:利用二叉搜索树的中序遍历结果是递增序列的性质,使用双指针在递增序列中查找两个数之和等于目标值。哈希表的核心思想是:遍历二叉搜索树,使用哈希表存储已访问的节点值,检查是否在哈希表中。在实际应用中,哈希表方法通常更受欢迎,因为它只需要遍历二叉树一次,而不需要存储整个遍历结果。

cannonjinx的博客 4372

【YOLO26科研级突破】集成BiFPN双向特征金字塔,让多尺度特征融合效率与精度双跃升

打开确保模型解析器能识别BiFPN模块。通过将BiFPN双向特征金字塔集成到YOLO26中,我们实现了一个“全尺度双向特征流动+加权融合+精准检测”的目标检测模型。这个改进不仅在工程上能大幅提升多尺度、复杂场景下的检测精度,更在科研上提供了“双向特征融合”的创新思路——这种思路可以延伸到其他检测网络(如Faster R-CNN、SSD)的Neck模块改进中,帮你在论文中打造独特的研究点。

LXY_LOVE36的博客 204

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 / \

淡然坊 2081

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

原题链接: https://leetcode.com/problems/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 t...

Ber03的博客 340

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

653. Two Sum IV - Input is a BST*

653. Two Sum IV - Input is a BST* https://leetcode.com/problems/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 ...

珍妮的选择的博客 299

LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。 LeetCode653. Two Sum IV - Input is a BST简单 案例 1: 输入: 5 / \ 3 6 / \ \ 2...

weixin_30430169的博客 116

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

【代码】LeetCode //C - 653. Two Sum IV - Input is a BST。

Made in Code 869

leetcode 653.两数之和-iv-输入-bst(two sum iv input is a bst)C语言

leetcode 653.两数之和-iv-输入-bst(two sum iv input is a bst)C语言1.description2.solution 1.description https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/ 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。 案例 1: 输入: Target = 9 输出: True 案例

hsk6543210的博客 250

653. Two Sum IV - Input is a BST

653. Two Sum IV - Input is a BST方法1: hash map方法2: two pointers 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 gi...

wilzxu的博客 133

[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

LeetCode653. Two Sum IV - Input is a BST

Leetcode 653. Two Sum IV - Input is a BST

正西风落叶下长安 873

LeetCode-653. Two Sum IV - Input is a BST [C++][Java]

Given therootof a Binary Search Tree and a target numberk, returntrueif there exist two elements in the BST such that their sum is equal to the given target.

贫道绝缘子的博客 261

【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

踏实 谦虚 卓越 640

LeetCode 653. Two Sum IV - Input is a BST

LeetCode 653. Two Sum IV - Input is a BST(Easy) 主要知识点:双指针、BST、BFS、DFS;优先级:4

温迪今天刷题了吗 276

Leetcode 653. Two Sum IV - Input is a BST

文章作者:Tyan 博客:noahsnail.com &nbsp;|&nbsp; CSDN &nbsp;|&nbsp; 简书 1. Description 2. Solution Version 1 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l...

SnailTyan 508

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

原题 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 Ta...

闲庭信步 325
上一篇: leetcode 652. Find Duplicate Subtrees(找出重复的子树)
下一篇: leetcode 654. Maximum Binary Tree(最大二叉树)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址